[英]change selection background of listview
在Android中,
我有使用以下代碼填寫的列表
@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”
任何實現這一目標的想法
創建選擇器的XML並將其保存在可繪制的文件夾中
<?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;
}
在ListView中提一下
android:listSelector =“ @ drawable / list_selector”
並將list_selector.xml放入drawable並粘貼此代碼,您可以根據需要進行更改...
<!-- 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 -->
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.