LinearLayout 上的选择器颜色
我正在尝试将颜色选择器分配给 LinearLayout 的扩展类,因此,我认为这就像我们谈论 LinearLayout 一样。
我按照这篇文章上的说明进行操作,答案谈论形状。
现在我在drawables文件夹上有3个xml:
normal.xml文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffffff" />
</shape>
pressed.xml文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#00000000" />
</shape>
最后是bg.xml文件
<?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>
我通过以下方式访问它:
Drawable d = getResources().getDrawable(context.getResources().getIdentifier("mypackageuri.tProject:drawable/bg", null, null));
view.setBackgroundDrawable(d);
“正常”状态很好,颜色设置为“normal.xml” “,但其他的没办法,我按我的观点,什么也没有发生,它没有以任何方式改变颜色......
我看不出我做错了什么......
谢谢
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
您的视图需要可点击,以便在单击它时获得
按下
状态。使用 :
或在布局 xml 中:
Your view needs to be clickable in order to get the state
pressed
when you click on it.Use :
or in the layout xml :