【问题标题】:Prevent highlight in list view防止在列表视图中突出显示
【发布时间】:2011-10-06 14:15:41
【问题描述】:

我有一个列表视图,我想防止在选择时突出显示。背景颜色在列表视图行布局上设置为半透明。我做得越透明,它就越会以 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>

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

【问题讨论】:

    标签: android listview highlight


    【解决方案1】:

    您是否尝试过为 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>
    

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

    【讨论】:

    • 谢谢,我试过了,包括使用 ListActivity 布局和列表项布局中的 android:listSelector="@background/backgroundlistcell" 选择器,但无论如何它似乎使项目完全透明我使用的透明度和/或颜色。我的代码中可能有一些东西会影响这一点,因为我正在从那里进行突出显示等。但是,我无法让它工作。
    【解决方案2】:

    您可以将 listSelector 定义为透明:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-21
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      相关资源
      最近更新 更多