【问题标题】:change selection background of listview更改列表视图的选择背景
【发布时间】:2012-05-08 09:51:25
【问题描述】:

在安卓中,

我有使用以下代码填写的列表

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.main, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
    textView.setText(values[position]);

    // Change icon based on name
    String s = values[position];

    System.out.println(s);

    if (s.equals("xxxxxxxx")) {
        imageView.setImageResource(R.drawable.one);
    } else if (s.equals("yyyyyyyyyy")) {
        imageView.setImageResource(R.drawable.two);
    } else if (s.equals("zzzzzzzzzzzzzzzzz")) {
        imageView.setImageResource(R.drawable.three);
    }

    // to change the background color


    return rowView;
}

如何更改选择颜色而不是橙色,在布局中我使用以下代码

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="#DFE1E5">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/one" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20dp" >
    </TextView>

</LinearLayout>

解决我这样做的问题

首先将颜色xml添加到drwable

<?xml version="1.0" encoding="utf-8"?> 

<resources> <color name="selection_color">#0587F4</color> 
</resources>

其次,我将 XML 添加到 drwable 以处理选择

<?xml version="1.0" encoding="utf-8"?>

<selector  xmlns:android="http://schemas.android.com/apk/res/android">

    <item 
        android:drawable="@color/selection_color"

        android:state_selected="true"/>
   <!--  <item android:drawable="@drawable/list_selection_bg" android:state_pressed="true"/>
--> 
</selector>

第一个问题是它引发了关于“@color/selection_color”的错误

任何实现这一目标的想法

【问题讨论】:

    标签: android


    【解决方案1】:

    创建Selector的XML并保存在drawable文件夹中

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

    然后在Adapter类的getView方法中添加如下代码

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        View rowView = inflater.inflate(R.layout.main, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        textView.setText(values[position]);
    
        // Change icon based on name
        String s = values[position];
    
        System.out.println(s);
    
        if (s.equals("xxxxxxxxxxx")) {
        imageView.setImageResource(R.drawable.one);
        } else if (s.equals("yyyyyyyy")) {
        imageView.setImageResource(R.drawable.two);
        } else if (s.equals("zzzzzzzzzzzzzzzzz")) {
        imageView.setImageResource(R.drawable.three);
        }
    
        // to change the background color
    rowView.setBackgroundResource(R.drawable.alternate_list_color1);
    
    
        return rowView;
    }
    

    【讨论】:

    • @drawable/list_selection_bg 在 R.drawable.alternate_list_color1 中指定选择颜色
    • 我很困惑,我需要将选择的颜色更改为 0587F4 并且 xml 的代码给出错误错误:错误:找不到与给定名称匹配的资源(在 'drawable' 中,值为 ' @drawable/list_selection_bg')。抱歉,我是 Android 新手
    • list_selection_bg是drawable文件夹中的列表选择图片,我们也可以在color.xml文件中通过颜色指定
    • 你能给我举例说明如何使用 color.xml 来使用 0587F4
    • #0587F4
    【解决方案2】:

    在 ListView 中提及

    android:listSelector="@drawable/list_selector"

    并将 list_selector.xml 设置为可绘制并粘贴此代码,您可以根据需要进行更改...

    <!-- Selected -->
    <item android:drawable="@drawable/list_hover" android:state_focused="true" android:state_pressed="false"/>
    <!-- @drawable/tab_focus -->
    
    
    <!-- Pressed -->
    <item android:drawable="@drawable/list_hover" android:state_pressed="true"/>
    <!-- @drawable/tab_press -->
    

    【讨论】:

      猜你喜欢
      • 2015-01-13
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      相关资源
      最近更新 更多