Android,setSelected() 和 state_selected

发布于 2024-12-07 19:59:25 字数 706 浏览 0 评论 0原文

我在使用 View.setSelected() 时遇到问题。 Views 被标记为已选择 - 例如,TextViews 更改其字体颜色 - 但我的背景选择器似乎没有注册更改。

示例选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/transparent" />

    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <solid android:color="#ff8600" />
        </shape>
    </item>
</selector>

我什至不确定哪种上下文信息有用。这些视图是 LinearLayout 的子级,我以编程方式在触摸事件内设置选定状态。正如我所说,它似乎确实有效,因为字体颜色从白色变为灰色,但背景保持不变。

编辑:我在发布之前检查了是否有愚蠢的错误:P。答案不是“添加 android:background 属性”。

I'm having trouble with View.setSelected(). Views are flagged as selected -- TextViews, for example, change their font color -- but my background selectors don't seem to register the change.

Example selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/transparent" />

    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <solid android:color="#ff8600" />
        </shape>
    </item>
</selector>

I'm not even sure which kind of context information would be useful. The views are children of a LinearLayout, and I'm programatically setting the selected state inside a touch event. As I said, it does seem to work, since the font color goes from white to gray, but the background stays the same.

Edit: I checked for silly mistakes before posting :P. The answer is not "add the android:background attribute".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

卸妝后依然美 2024-12-14 19:59:25

项目的顺序在选择器 xml 中很重要,默认项目应始终位于项目列表的底部。

The order of items matters in selector xmls, the default item should always be at the bottom in the items list.

烟─花易冷 2024-12-14 19:59:25

不仅所选状态的顺序很重要,甚至所有状态的顺序也很重要。就我而言,我首先添加了 state_pressed ,但我的 state_selected 不起作用。所以我像这样改变了顺序,然后它起作用了:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
  <item 
    android:state_selected="false"
    android:drawable="@drawable/chooser_normal"></item>
<item 
    android:state_selected="true"
    android:drawable="@drawable/chooser_pressed"></item>
<item 
    android:state_pressed="true"
    android:drawable="@drawable/chooser_pressed"></item>
<item 
    android:state_pressed="false"
    android:drawable="@drawable/chooser_normal"></item>
 </selector>

编辑

我现在也面临这个问题,如果我按下按钮,它将处于选定状态,但不会处于按下状态。因此,解决方案必须是,要对这样的状态和附加状态进行排序,添加默认按钮外观是一个很好的做法:

首先,设置选择状态,然后交替设置与按下状态相同的状态。 (目前,stackoverflow 没有完全显示我的编辑,不知道为什么,请耐心等待)。

not only the selected state order is important, even the order of all states is important. In my case, I added state_pressed as first and my state_selected doesn´t work. So I changed the order like this, and then it worked:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
  <item 
    android:state_selected="false"
    android:drawable="@drawable/chooser_normal"></item>
<item 
    android:state_selected="true"
    android:drawable="@drawable/chooser_pressed"></item>
<item 
    android:state_pressed="true"
    android:drawable="@drawable/chooser_pressed"></item>
<item 
    android:state_pressed="false"
    android:drawable="@drawable/chooser_normal"></item>
 </selector>

EDIT

I faced now also the problem, if I press the button, it will be in the selected-state but not in pressed-state. So, the solution must be, to order the states like this and additional, it´s a good practise to add a default button look:

First, set the selected state and after this, set the same as pressed state alternately. (For now, stackoverflow isn´t showing my edit completely, don´t know why, just be patient please).

魄砕の薆 2024-12-14 19:59:25

原因并不明显,但我认为这可能有效(注意添加了 state_selected="false"):

<item android:state_selected="false" android:drawable="@android:color/transparent" />

<item android:state_selected="true">
    <shape android:shape="rectangle">
        <solid android:color="#ff8600" />
    </shape>
</item>

我希望这有帮助。

It's not obvious why, but I think this might work (notice the addition of state_selected="false"):

<item android:state_selected="false" android:drawable="@android:color/transparent" />

<item android:state_selected="true">
    <shape android:shape="rectangle">
        <solid android:color="#ff8600" />
    </shape>
</item>

I hope that's helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文