这是我的列表视图代码:
<?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="@color/darkgrey">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</LinearLayout>
这是列表选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:drawable="@drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/gradient_bg_hover" />
<item android:drawable="@drawable/gradient_bg" />
</selector>
我的目标是为列表项添加背景:初始(没有发生点击,没有焦点) - &gt;灰色的背景。按下时 - &gt;蓝色背景。发布时 - &gt;灰色背景。
但最初的背景并没有出现。打开应用程序时以及尚未单击某个项目时,灰色背景不会显示。有人知道如何解决这个问题吗?
java部分:
package com.example.whs;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class Index extends Activity {
public static final Object TITLE = "title";
public static final Object SUBTITLE = "subtitle";
public static final Object THUMBNAIL = "thumbnail";
protected static final String POSITION = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
// Back actionbar icon enable
//ActionBar actionBar = getActionBar();
//actionBar.setDisplayHomeAsUpEnabled(true);
buildMenu();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.index, menu);
return true;
}
//Builds the menu for listview
public void buildMenu(){
ArrayList<HashMap<String, String>> menu = new ArrayList<HashMap<String, String>>();
//Arrays for info
String[] menuTitleArray = {"Updates", "Gallerij"};
String[] menuSubtitleArray = {"Bekijk updates", "Bekijk foto's en geef reacties", "Bekijk de updates"};
String[] menuThumbnailArray = {"updates", "gallery"};
for(int i=0; i < menuTitleArray.length; i++){
// Build Hashmap for the item
HashMap<String, String> item = new HashMap<String, String>();
item.put((String) TITLE, menuTitleArray[i]);
item.put((String) SUBTITLE, menuSubtitleArray[i]);
item.put((String) THUMBNAIL, menuThumbnailArray[i]);
menu.add(item);
}
// Add adapter to the list
MenuAdapter adapter = new MenuAdapter(this, menu);
ListView list = (ListView)findViewById(R.id.list);
list.setAdapter(adapter);
// Initialize the click event
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
switch(position){
case 0:
Intent intent = new Intent(Index.this, Updates.class);
startActivity(intent);
}
}
});
}
}
答案 0 :(得分:1)
我想这是因为列表行的布局xml有自己的背景设置。然后你的listSelector成功设置但被覆盖:)
答案 1 :(得分:0)
使用您android:listSelector="@drawable/list_selector"
custom item layout
项ListView
中的ListView
。
就像你在这里<?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="@color/darkgrey">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
/>
</LinearLayout>
<?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:listSelector="@drawable/list_selector">
</LinearLayout>
它正在使用以下布局
<item android:drawable="@drawable/button_bg_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/button_bg_normal"></item>
修改强>
你也可以使用像
这样的选择器{{1}}
答案 2 :(得分:0)
我想你希望在列表行上添加选择器,而不是在ListView上添加:) 以下是您的link。