Android LinearLayout 选择器背景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21273510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:15:48  来源:igfitidea点击:

Android LinearLayout Selector background color

androidlayoutcolorsbackgroundselector

提问by Peter

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't work.

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

I looked for solutions and all they say was add clickable attribute. I've already done that.

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

My LinearLayout contains two LinearLayout which contains 9 TextViews each. They fill my LinearLayout fully.

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

What I thought of was that its child is absorbing the click event and my LinearLayout doesn't change its state to pressed.

我想到的是它的孩子正在吸收点击事件,而我的 LinearLayout 不会将其状态更改为按下。

So I put clickable and focusalbe attribute to false on every child of my LinearLayout.

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

However, it's still same.

然而,它仍然是一样的。

Here's my code.

这是我的代码。

This is the selector jbbtn.xml

这是选择器 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>

And This is My LinearLayout

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

回答by minelight

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:

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

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>

回答by user6228151

You can add:

你可以加:

android:clickable="true"

to LinearLayout.

LinearLayout

回答by mayank1513

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

虽然这个问题有答案,但我写这篇文章是为了澄清并给出我的观察。

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

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

The second answer actually completes the first one.

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