Using LinearLayout as a button, I want to change the background color of linear layout by time, and show the different states by different colors, I want to realize it by selector, but it's completely ineffective.
I looked at the other solutions and said I needed to add the click attribute time, but my code has already added
My LinearLayout contains 2 small linearlayout, not a small linearlayout and contains 9 TextView, these 18 textview filled my layout.
My idea is that each textview is clicked to change the linearlayout background color.
I've added a click event to all the TextView, but it still doesn't work.
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/JB Btn_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_wid Th= "Wrap_content" android:layout_height= "Match_parent" android:layout_alignparentbottom= "true" Android:layout_ali Gnparentleft= "true" android:layout_aligntop= "@+id/lltimer" android:layout_below= "@id/btnmenu" Android:layout_marg inright= "3SP" android:clickable= "true" android:focusable= "true" android:orientation= "Horizontal" Android:paddi ng= "3SP" > ~~~~~~ </LinearLayout>
Processing methods
I also use linear layout as a button, I do not bind any click events, it will work, I set a style for the standard button, I just keep assigning styles to buttons
Think of LinearLayout as a 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 La Bel "/> <textview android:id=" @+id/tvllbutton "android:layout_height=" 0px "android:layout_w eight= "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>
My @drawable/btn_stan_sel file:
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android > <!--disabled state-to <item android:drawable= "@drawable/btn_stand_disabled" Android : state_enabled= "false"/> <!--enabled and pressed state-to- <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-to- <item android:drawable= "@drawable/btn_stand_ Enabled "android:state_enabled=" true "/> </selector>
My drawing file is repeated for each state to draw a different color
<?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>
Original address: http://www.itmmd.com/201411/186.html
This article by Meng Meng's IT person to organize the release, reprint must indicate the source.
Selector background color in Android LinearLayout