防止在列表视图中突出显示

发布于 2024-11-23 18:24:20 字数 1492 浏览 2 评论 0原文

我有一个列表视图,我想在其中阻止突出显示选择。列表视图行布局上的背景颜色设置为半透明。我让它越透明,它就越以 Android 默认的橙色突出显示。

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/CodeFont"
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="35sp" 
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:paddingLeft="20dip" 
    android:paddingTop="10dip" 
    android:paddingBottom="10dip"
    android:cacheColorHint="#00000000" 
    android:background="#AAEBD5A8"

/>

如果我删除背景并像这样设置颜色,

private void makeListDefault(ListView listView) {
    int childCount = listView.getChildCount();
    int i = 0;
    for (i = 0; i < childCount; i++) {
        View view = listView.getChildAt(i);
        view.setBackgroundResource(R.drawable.hilight_no_gradient_correct_drawable);
    }
}

它不会以任何方式影响颜色。 hilight_no_gradient_ Correct_drawable

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#FF98FB98" android:endColor="#FF98FB98"
        android:angle="90" />
        <!-- 98FB98 is a shade of green -->
</shape>

有什么想法吗?我也用选择器尝试过,但似乎没有什么影响颜色。

I have a list view where I'd like to prevent the highlight on selection. The background color is set to be sem-transparent on the list view row layout. The more transparent I make it it, the more it gets highlighted in Android's default orange color.

Here's the layout:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/CodeFont"
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="35sp" 
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:paddingLeft="20dip" 
    android:paddingTop="10dip" 
    android:paddingBottom="10dip"
    android:cacheColorHint="#00000000" 
    android:background="#AAEBD5A8"

/>

If I remove the background and set the color like this,

private void makeListDefault(ListView listView) {
    int childCount = listView.getChildCount();
    int i = 0;
    for (i = 0; i < childCount; i++) {
        View view = listView.getChildAt(i);
        view.setBackgroundResource(R.drawable.hilight_no_gradient_correct_drawable);
    }
}

It's doesn't affect the color in any way. The hilight_no_gradient_correct_drawable

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#FF98FB98" android:endColor="#FF98FB98"
        android:angle="90" />
        <!-- 98FB98 is a shade of green -->
</shape>

Any ideas? I've also tried it with a selector, but nothing seems to affect the color.

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

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

发布评论

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

评论(2

[浮城] 2024-11-30 18:24:20

您可以将 listSelector 定义为透明,如下所示:

android:listSelector="@android:color/transparent"

它将阻止突出显示/选定状态的默认颜色。

You can define listSelector as Transparent as:

android:listSelector="@android:color/transparent"

It will prevent the default color for the highlighted/selected state.

笙痞 2024-11-30 18:24:20

您是否尝试过为 ListView 的背景项使用自定义颜色?

<ListView
    android:id="@+id/conversations"
    android:scrollbars="vertical"
    android:layout_width="fill_parent"
    android:cacheColorHint="#000000"
    android:visibility="visible"
    android:listSelector="@color/backgroundlistcell"
    android:dividerHeight="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</ListView>

请注意listSelector =“....”

backgroundlistcell.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_focused="false"
    android:state_pressed="false"
    android:state_selected="false"
    android:state_enabled="true"
    android:drawable="@drawable/listcell_enabled" />
<item
    android:state_focused="true"
    android:drawable="@null" />
<item
    android:state_selected="true"
    android:drawable="@null" />
<item
    android:state_pressed="true"
    android:drawable="@drawable/listcell_pressed" />
</selector>

也许您需要更改drawable属性值但这应该可行。

Did you try with a custom color for background item of a ListView ?

<ListView
    android:id="@+id/conversations"
    android:scrollbars="vertical"
    android:layout_width="fill_parent"
    android:cacheColorHint="#000000"
    android:visibility="visible"
    android:listSelector="@color/backgroundlistcell"
    android:dividerHeight="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</ListView>

be aware of listSelector="...."

backgroundlistcell.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_focused="false"
    android:state_pressed="false"
    android:state_selected="false"
    android:state_enabled="true"
    android:drawable="@drawable/listcell_enabled" />
<item
    android:state_focused="true"
    android:drawable="@null" />
<item
    android:state_selected="true"
    android:drawable="@null" />
<item
    android:state_pressed="true"
    android:drawable="@drawable/listcell_pressed" />
</selector>

Maybe you will need to change drawable attribute value but this should work.

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