Android LinearLayout Selector background color

后端 未结 3 1957
攒了一身酷
攒了一身酷 2021-02-13 09:37

Hi I\'m trying to make my linear layout work like button. I mean I\'m trying to change its background color when the state is changed. I used selector to solve it, but it didn\'

相关标签:
3条回答
  • 2021-02-13 10:02

    I am using a linear layout as a button however, I do not have anything assigned as clickable or not and it seems to work just fine. I have set up a style for my standard button and I just assign that style to the linear layout like I would any other button.

    The 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 drawable file repeated for each state just with different colors for each state:

    <?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>
    
    0 讨论(0)
  • 2021-02-13 10:22

    Although this question has answer I am writing this to clarify and give my observation.

    answer by @minelight does not work on views that are not clickable, for example LinearLayout and TextView. The statePressed state is available only for clickable views.

    The second answer actually completes the first one.

    0 讨论(0)
  • 2021-02-13 10:24

    You can add:

    android:clickable="true"
    

    to LinearLayout.

    0 讨论(0)
提交回复
热议问题