【问题标题】:Selector color on LinearLayoutLinearLayout 上的选择器颜色
【发布时间】: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


【解决方案1】:

您的视图需要是可点击的,以便在您点击它时获得pressed 状态。 使用:

    view.setClickable(true);

或在布局xml中:

    android:clickable="true"

【讨论】:

  • 就这么简单...谢谢!我明天试试!
猜你喜欢
  • 2014-02-11
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 1970-01-01
相关资源
最近更新 更多