Selector background color in Android LinearLayout, androidselector

Source: Internet
Author: User

Selector background color in Android LinearLayout, androidselector

Using linearLayout as a button, I want to use time to trigger dynamic changes to the background color of linear layout and display different States through different colors. I want to use selector to achieve this, but it has no effect at all.

After reading other solutions, I said that the click attribute time needs to be added, but my code has already been added.

My LinearLayout contains two small LinearLayout items. No small LinearLayout contains nine textviews. These 18 textviews fill up my layout.

My idea is to change the background color of LinearLayout when each TextVIew is clicked.

I added click events to all textviews, but they still do not 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/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>


Solution

I also use linear layout as a button. I didn't bind any click events, and it will work. I created a style for the standard button, and I just kept assigning styles to the buttons.

Use 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 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>


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 -->    <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>


My drawing files are repeated to draw different colors for each status

<?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>


Address: http://www.itmmd.com/201411/186.html
This article is organized and published by Meng IT personnel. The reprinted article must indicate the source.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.