2014-01-22 98 views
12

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

我寻找的解决方案,他们都说是添加可点击的属性。 我已经这样做了。

我的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> 
+0

您是否将jbbtn.xml设置为'LinearLayout'的背景? “没有工作”是什么意思? – Emmanuel

+0

在你的LinearLayout中试试这个android:descendantFocusability =“blocksDescendants”。 –

+0

@Emmanuel Yep,有什么问题..?当我寻找解决方案时,我只看到其他人这样做:( – Peter

回答

14

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

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

我的风格定义按钮:

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

我@绘制/ 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> 
6

您可以添加:

android:clickable="true" 

LinearLayout

+0

但你没有选择器可点击 – murt

+0

不工作:( –