Android LinearLayout选择器背景颜色

时间:2014-01-22 03:18:07

标签: android layout colors background 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>

这是我的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>

3 个答案:

答案 0 :(得分:14)

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

线性布局作为按钮:

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

按钮的样式定义:

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

答案 1 :(得分:6)

您可以添加:

android:clickable="true"

LinearLayout

答案 2 :(得分:1)

尽管这个问题有答案,但我正在写这篇文章以澄清并给出我的观察。

@minelight提供的

answer不适用于不可点击的视图,例如LinearLayout和TextView。 statePressed状态仅适用于可点击的视图。

第二个答案实际上完成了第一个答案。