android TextView中圖片和文字的灰顯
阿新 • • 發佈:2019-01-22
在某種情況下,menu的某一子項(圖示和文字)要求不能點選並且灰顯。
1. menu子項
menu_item.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/textview_enable" android:textSize="@dimen/menu_item_text_size" android:drawablePadding="@dimen/menu_item_drawable_padding" android:paddingTop="@dimen/menu_item_padding_top" android:paddingBottom="@dimen/menu_item_padding_bottom" android:paddingLeft="@dimen/menu_item_padding_left" android:gravity="left|center_vertical" android:background="@drawable/menu_item_style" > </TextView>
其中設定灰顯程式碼如下:
TextView tv = (TextView)convertView;
tv.setCompoundDrawables(R.drawable.menu_hide_apps, null, null, null);//設定圖示,放在文字左邊
tv.setEnabled(false);
2.文字灰顯
文字顏色使用android:textColor="@color/textview_enable",如下
textview_enable.xml
其中上面一個item為灰顯顏色。<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="false" android:color="#FF4F4F4F" /> <item android:color="@color/white"/> </selector>
3.圖示灰顯
res/drawable/menu_hide_apps.xml
其中上面一個item為灰顯圖示。<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="false" android:drawable="@drawable/menu_hide_apps_disable" /> <item android:drawable="@drawable/menu_hide_apps_normal"/> </selector>