Selector color on LinearLayout

江枫思渺然 提交于 2019-12-21 10:34:58

问题


I'm trying to assing a color selector to an extended class of LinearLayout, so, i think its like if we speak about linearLayout.

i followed the instructions on this post, the answer talking about shapes.

Now i have 3 xml on drawables folders:

normal.xml file

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >
  <solid android:color="#ffffffff" />
</shape>

pressed.xml file

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
   <solid android:color="#00000000" />
</shape>

and finally, bg.xml file

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/pressed" />
    <item android:state_focused="true" android:drawable="@drawable/pressed" />
    <item android:state_selected="true" android:drawable="@drawable/pressed" />
    <item android:drawable="@drawable/normal" />
</selector>

I am accessing this in the following way:

    Drawable d = getResources().getDrawable(context.getResources().getIdentifier("mypackageuri.tProject:drawable/bg", null, null));
    view.setBackgroundDrawable(d);

The "normal" state its fine, with the color set at "normal.xml", but no way with the other ones, I press my view and nothing happens, it's not changing color in any way...

I can't see what i'm doing wrong...

Thank you


回答1:


Your view needs to be clickable in order to get the state pressed when you click on it. Use :

    view.setClickable(true);

or in the layout xml :

    android:clickable="true"


来源:https://stackoverflow.com/questions/6865730/selector-color-on-linearlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!