【问题标题】:android listview semitransparent selection color of a rowandroid listview半透明选择一行的颜色
【发布时间】:2011-06-01 11:55:45
【问题描述】:

我需要在列表视图中实现对行的半透明选择,以及“按下”状态。

如果我应用纯色,那么一切都按预期工作。但是,如果我应用半透明颜色 (#44444444),那么我会看到默认选择颜色(在我的 2.3 android 上为橙色),并且在它上面是我的颜色(它会使橙色变暗一点)。

为什么我的下面有橙色? 如何解决这个问题?

这是我的代码: drawable/listselectorinvisible.xml中的选择器xml

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

<item android:state_pressed="true" 
    android:drawable="@color/semitransparent" />

<item android:state_selected="true" android:state_pressed="false"
    android:drawable="@color/semitransparent" />
</selector>

Listview 行在 layout/topscore_row.xml 中定义

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent"
android:background="@drawable/listselectorinvisible">
  <TextView android:layout_height="wrap_content" android:id="@+id/scrowNum" android:textColor="@color/fontButColor" android:text="#" android:textSize="24sp" android:layout_width="32dip" android:gravity="right|top" android:layout_gravity="top"></TextView>
  <LinearLayout android:layout_height="wrap_content" android:id="@+id/scrowNamLay" android:layout_width="142dip" android:orientation="vertical">
    <TextView android:layout_height="wrap_content" android:paddingTop="2dip" android:layout_width="fill_parent" android:id="@+id/scrowPlayer" android:textColor="@color/fontButColor" android:text="@string/tblPlayer" android:textSize="24sp" android:paddingLeft="2dip"></TextView>
    <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/scrowOpPlayer" android:textColor="@color/fontButColor" android:text="@string/tblPlayer" android:textSize="14sp" android:paddingLeft="2dip"></TextView>
  </LinearLayout>
  <ImageView android:id="@+id/scrowImg" android:layout_height="wrap_content" android:src="@drawable/small_im_bt" android:padding="2dip" android:layout_marginTop="2dip" android:layout_width="26dip"></ImageView>
  <TextView android:layout_height="wrap_content" android:layout_width="48dip" android:id="@+id/scrowScore" android:layout_marginRight="5dip" android:gravity="right" android:textColor="@color/fontButColor" android:text="@string/tblScore" android:textSize="26sp"></TextView>
  <TextView android:layout_height="wrap_content" android:id="@+id/scrowTime" android:textColor="@color/fontButColor" android:text="@string/tblTime" android:gravity="center_horizontal" android:textSize="26sp" android:layout_width="58dip"></TextView>
</LinearLayout>

最后是列表视图本身:

<ListView android:layout_width="match_parent" android:id="@+id/scoreList" android:paddingLeft="5dip" android:paddingRight="5dip" 
  android:cacheColorHint="#00000000" 
  android:choiceMode="none" 
  android:clickable="false" 
  android:focusable="false" 
  android:focusableInTouchMode="false" 
  android:longClickable="false" android:layout_height="298dip">
</ListView>

在 xml 中设置颜色失败后,我还尝试通过 convertView.setBackgroundResource(R.drawable.listselectorinvisible); 在我的 CustomArrayAdapter 中设置 listselectorinvisible; 但没有运气。

CustomAdapter 的代码:

        /* (non-Javadoc)
     * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v = convertView;
        if (v == null) 
        {
            LayoutInflater vi = (LayoutInflater)app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.topscore_row, null);
        }

        Score score = objects.get(position);
        int color = getColor(position, score);

        if (score != null) 
        {
            ImageView iv = (ImageView) v.findViewById(R.id.scrowImg);
            if (iv != null) 
            {
                iv.setImageResource(imgs[score.gameType]);
            }
            TextView tv = (TextView) v.findViewById(R.id.scrowNum);
            tv.setText(Integer.toString(position+1) + ".");
            tv.setTypeface(app.mainFont);
            tv.setTextColor(color);
            .... ....               
        }

        v.setBackgroundResource(R.drawable.listselectorinvisible);

        return v;

    }               

提前谢谢你。

【问题讨论】:

    标签: android listview android-listview


    【解决方案1】:

    你必须在 ListView 中设置android:listSelector="@drawable/listcolor"

    您的 ListView 将是

    <ListView android:layout_width="match_parent" android:id="@+id/scoreList" android:paddingLeft="5dip" android:paddingRight="5dip" 
      android:cacheColorHint="#00000000" 
      android:choiceMode="none" 
      android:clickable="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:longClickable="false" 
    android:listSelector="@drawable/listcolor"
    android:layout_height="298dip">
    </ListView>
    

    看看下面的网址

    How to set Selection Color to ListView

    谢谢 迪帕克

    【讨论】:

    • 很有趣,谢谢。我想知道它是否不会因为它抑制 cacheColorHint 的优化机制而减慢速度。今晚我会尝试北方船长建议的半透明颜色,我喜欢这个设计。
    • 感谢您的建议。我刚刚在模拟器上尝试过,橙色消失了,我只看到我的半透明。但是当我按下列表中的项目时,这种颜色也适用于整个列表。我没有在真机上试用过,不要随身携带,可能是模拟器的BUG...
    • @Snicolas - 我也用 addStatesFromChildren 试过你的代码,但它没有任何改变
    • 在真实设备上检查了 android:listSelector 属性并得到了相同的效果:在按下状态时,选择器颜色应用于整个列表视图,而不仅仅是按下的行。有什么想法吗?
    【解决方案2】:

    在同一个问题上玩了几个小时后,我终于找到了解决方案。

    首先,在 ListView 中设置 listSelector="@android:color/transparent" (由于某种原因@null 在这里不起作用)

    其次,让你的行的最父背景成为 state_pressed="@color/with_transparency" 的有状态可绘制对象(可能是有状态颜色)

    【讨论】:

    • 我想补充一点,将列表背景颜色设置为纯色是安全的,我认为大多数人都希望这样做
    【解决方案3】:

    我不知道这是否重要,但您的选择器没有默认状态(应该是最后一个)。

    另外,它没有回答你的问题,但是 在 top_score 行中,考虑将以下内容添加到您的 xml 中,添加到您的根布局中:

    android:addStatesFromChildren="true" 
    

    它会说捕获子项状态的列表项,即复合组件充当单个组件并获取内部任何组件的状态。 (例如,如果一个被选中,那么复合组件就会被选中)。

    问候, 斯蒂芬

    【讨论】:

      猜你喜欢
      • 2012-08-14
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多