android layout color,Android LinearLayout Selector background color

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.

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

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

However, it's still same.

Here's my code.

This is the selector jbbtn.xml

android:drawable="@drawable/jbbtn_pressed"/>

android:drawable="@drawable/jbstyle_transparent"/>

And This is My 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" >

~~~~~~

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

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:

style="@style/btn_stand"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:onClick="onClickLLButton"

android:orientation="vertical" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:text="Button Label" />

android:id="@+id/tvLLButton"

android:layout_height="0px"

android:layout_weight="1"

android:gravity="center"

android:text="Button Info" />

My style definition for the button:

@drawable/btn_stand_sel

@drawable/btn_stand_text_color

48dp

5dp

5dp

My @drawable/btn_stan_sel file:

My drawable file repeated for each state just with different colors for each state:

android:shape="rectangle" >

android:width="1dp"

android:color="@color/stroke" />

# Answer 2

You can add:

android:clickable="true"

to LinearLayout.

# Answer 3

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值