如果背景不透明,ListViewItem 不会突出显示

发布于 2024-09-25 04:01:31 字数 496 浏览 4 评论 0原文

我有一个带有自定义渐变背景的 ListViewItem。默认选择器不再突出显示这些行。

仅当我将背景设置为透明时,突出显示才有效。如果没有这种改变,我怎样才能获得亮点?

这是我的列表视图:

<ListView android:id="@+id/symbolsListView"
    android:layout_width="fill_parent"
    android:background="@drawable/transparent_background"
    android:layout_height="390dp"
    android:divider="@drawable/ui_divider_line"
    android:cacheColorHint="#00000000"
    android:listSelector="@drawable/blue"
    android:drawSelectorOnTop="true"
    >

I have a ListViewItem with a custom gradient background. The default selector no longer highlights these rows anymore.

Highlight only works if I set the background to transparent. How can I get the highlight without this change?

Here is my ListView:

<ListView android:id="@+id/symbolsListView"
    android:layout_width="fill_parent"
    android:background="@drawable/transparent_background"
    android:layout_height="390dp"
    android:divider="@drawable/ui_divider_line"
    android:cacheColorHint="#00000000"
    android:listSelector="@drawable/blue"
    android:drawSelectorOnTop="true"
    >

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

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

发布评论

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

评论(3

吃兔兔 2024-10-02 04:01:31

你的一些状态是聚焦的和 window_focused 的。下面我提到了选择器的代码,试试这个。

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

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

    <!--
        Even though these two point to the same resource, have two states so
        the drawable will invalidate itself when coming out of pressed state.
    -->
    <item android:state_focused="true" android:state_enabled="false"
        android:state_pressed="true" android:drawable="@color/transparent" />
    <item android:state_focused="true" android:state_enabled="false"
        android:drawable="@color/transparent" />

    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@color/solid_red" />
    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@color/solid_red" />

    <item android:state_focused="true" android:drawable="@color/solid_red" />

</selector>

希望它有效。

your some states that are focused and window_focused. Below i mentioned the code for selector try this.

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

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

    <!--
        Even though these two point to the same resource, have two states so
        the drawable will invalidate itself when coming out of pressed state.
    -->
    <item android:state_focused="true" android:state_enabled="false"
        android:state_pressed="true" android:drawable="@color/transparent" />
    <item android:state_focused="true" android:state_enabled="false"
        android:drawable="@color/transparent" />

    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@color/solid_red" />
    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@color/solid_red" />

    <item android:state_focused="true" android:drawable="@color/solid_red" />

</selector>

Hope it works.

鱼忆七猫命九 2024-10-02 04:01:31

ListView 中的当前选择由您使用“listSelector”属性提供的附加可绘制对象突出显示。

默认情况下,选择器可绘制对象绘制在列表项后面。因此,如果 yout item 的渐变背景是不透明的,则选择器将永远不会显示。

有 2 个解决方案:

  • 通过降低 alpha 值使背景渐变透明。
  • 将“drawSelectorOnTop”属性设置为 true,以便选择器绘制在列表项的顶部。在这种情况下,请确保您的选择器可绘制部分不是完全不透明的。

The current selection in a ListView is highlighted by an additional drawable that you provide using the "listSelector" attribute.

By default, the selector drawable is drawn behind the list item. Therefore, if the gradient background of yout item is opaque, the selector will never be shown.

There are 2 solutions :

  • Make the background gradient transparent by lowering alpha values.
  • Set the "drawSelectorOnTop" attribute to true, so that the selector is drawn on top of the list item. In this case, make sure your selector drawable is not fully opaque.
不即不离 2024-10-02 04:01:31

使用 显示带有渐变背景的“按下”状态。

Use a <selector> to display a "pressed" state with your gradient background.

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