標籤:android android開發
把linearLayout 當成按鈕使用,我想通過時間觸發動態改變 linear layout 的背景顏色,通過不同的顏色展示不同的狀態, 我想通過selector來實現,但是完全沒有效果
我看了下其他的解決方案,都說需要添加點擊屬性時間,但是My Code已經添加了
我的 LinearLayout 包含了2個小的 LinearLayout ,沒個小LinearLayout 又包含了9個TextView,這18個TextVIew把我的布局填充滿了.
我的想法是,每個TextVIew被點擊的時候改變 LinearLayout 的背景顏色。
我把所有的Textview都添加了 點擊事件,但是還是不起效果。
selector jbbtn.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/jbbtn_pressed"/> <item android:state_enabled="true" android:drawable="@drawable/jbstyle_transparent"/> <item android:state_enabled="false" android:drawable="@drawable/jbbtn_disabled"/></selector> My LinearLayout<LinearLayout android:id="@+id/llCurrents" android:background="@drawable/jbbtn" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignTop="@+id/llTimer" android:layout_below="@id/btnMenu" android:layout_marginRight="3sp" android:clickable="true" android:focusable="true" android:orientation="horizontal" android:padding="3sp" > ~~~~~~ </LinearLayout>
處理方法
我也是用線性布局當做一個按鈕,我沒有綁定任何點擊事件,它就能夠工作,我為標準的按鈕建立了一個樣式,我只是不停的分配樣式到按鈕
把 linearlayout 當成一個 button:
<LinearLayout style="@style/btn_stand" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClickLLButton" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Button Label" /> <TextView android:id="@+id/tvLLButton" android:layout_height="0px" android:layout_weight="1" android:gravity="center" android:text="Button Info" /></LinearLayout>My style definition for the button:<style name="btn_stand" parent="AppBaseTheme"> <item name="android:background">@drawable/btn_stand_sel</item> <item name="android:textColor">@drawable/btn_stand_text_color</item> <item name="android:minHeight">48dp</item> <item name="android:paddingLeft">5dp</item> <item name="android:paddingRight">5dp</item></style>
我的@drawable/btn_stan_sel 檔案:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- disabled state --> <item android:drawable="@drawable/btn_stand_disabled" android:state_enabled="false"/> <!-- enabled and pressed state --> <item android:drawable="@drawable/btn_stand_pressed" android:state_enabled="true" android:state_pressed="true"/> <!-- enabled and focused state --> <item android:drawable="@drawable/btn_stand_focused" android:state_enabled="true" android:state_focused="true"/> <!-- enabled state --> <item android:drawable="@drawable/btn_stand_enabled" android:state_enabled="true"/> </selector>
我的繪製檔案為重複的為每一個狀態繪製不同的顏色
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <stroke android:width="1dp" android:color="@color/stroke" /> <solid android:color="@color/blue" /> <corners android:radius="6dp" /> </shape>
原文地址:http://www.itmmd.com/201411/186.html
該文章由 萌萌的IT人 整理髮布,轉載須標明出處。
Android的LinearLayout中 selector背景顏色