【发布时间】:2011-07-28 21:11:57
【问题描述】:
我正在尝试将颜色选择器分配给 LinearLayout 的扩展类,因此,我认为这就像我们谈论线性布局一样。
我按照this 帖子上的说明进行操作,答案是关于形状。
现在我在 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”,但其他的不行,我按下我的视图并没有任何反应,它没有以任何方式改变颜色......
我看不出我做错了什么......
谢谢
【问题讨论】:
-
可能要粘贴您的布局 xml
-
哇,这是获取可绘制资源的最迂回方式。而不是
getResources().getDrawable(context.getResources().getIdentifier("mypackageuri.tProject:drawable/bg", null, null));尝试getResources().getDrawable(R.drawable.bg);。除非我错过了什么。 -
@dmon 好点,我也试试!谢谢!!
-
如果有人想获得完整的解决方案,请检查此存储库:github.com/shamanland/AndroidLayoutSelector 有自定义可点击/可检查
LinearLayout就像ToggleButton
标签: android colors android-linearlayout