Android LinearLayout选择器背景颜色

Pet*_*ter 13 layout android background colors selector

嗨,我想让我的线性布局像按钮一样工作.我的意思是当状态改变时我试图改变它的背景颜色.我使用了选择器来解决它,但它没有用.

我寻找解决方案,他们说的只是添加可点击属性.我已经做到了.

我的LinearLayout包含两个LinearLayout,每个包含9个TextView.它们完全填满了我的LinearLayout.

我想到的是它的孩子正在吸收点击事件,而我的LinearLayout并没有将其状态改为按下状态.

所以我在我的LinearLayout的每个子节点上都将clickable和focusalbe属性设置为false.

但是,它仍然是一样的.

这是我的代码.

这是选择器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>
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

小智 15

我使用线性布局作为按钮,然而,我没有任何指定为可点击的东西,它似乎工作得很好.我已经为我的标准按钮设置了一个样式,我只是将该样式分配给线性布局,就像我任何其他按钮一样.

linearlayout作为按钮:

<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>
Run Code Online (Sandbox Code Playgroud)

我按钮的样式定义:

<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>
Run Code Online (Sandbox Code Playgroud)

我的@ 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>
Run Code Online (Sandbox Code Playgroud)

我的可绘制文件针对每种状态重复,每种状态只有不同的颜色:

<?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>
Run Code Online (Sandbox Code Playgroud)


小智 6

你可以加:

android:clickable="true"
Run Code Online (Sandbox Code Playgroud)

到LinearLayout.