Android listview高亮问题
我必须列出物品清单。这是我的布局 XML:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="#FFFFFF"
android:stretchColumns="0">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:id="@+id/listbox">
<LinearLayout
android:layout_width="fill_parent"
android:background="#FFFFFF"
android:layout_gravity="center_horizontal"
android:layout_height="fill_parent">
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
<LinearLayout
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:background="#FFFFFF"
android:layout_gravity="center"
android:layout_height="fill_parent">
<TextView
android:id="@+id/emptytext"
android:paddingTop="100dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#818185"
android:textSize="25sp"
android:textStyle="bold"
android:background="#FFFFFF"
android:text="@string/empty_fav"
android:gravity="center_horizontal|center_vertical"
android:paddingLeft="6dip"
/>
</LinearLayout>
</LinearLayout>
</TableRow>
</TableLayout>
它是 R.layout.custom_list_item_1 的 xml。
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold"
android:background="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
这是我的代码。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fav_layout);
....
........
wordDataHelper = new WordDataHelper(getApplicationContext());
favCursor = wordDataHelper.getCursorFav();
startManagingCursor(favCursor);
favAdapter = new SimpleCursorAdapter(
this,
R.layout.custom_list_item_1,
favCursor,
new String[] {WordDataHelper.ENGWORD},
new int[] {android.R.id.text1});
setListAdapter(favAdapter);
list = getListView();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
showVideo(((TextView) view).getText());
}});
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
// @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Context Menu");
menu.add(0, CONTEXT_DELETE, 1, "Delete Item");
}
});
list.setTextFilterEnabled(true);
list.setClickable(true);
}
现在一切都显示良好,所有其他功能也都正常 工作正常。但如果我触摸任何行,它不会突出显示 即颜色没有改变。我的代码有问题还是 是否必须单独实施?如果是这样,怎么办?
I have to do a listing for items. Here is my XML for layout:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="#FFFFFF"
android:stretchColumns="0">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:id="@+id/listbox">
<LinearLayout
android:layout_width="fill_parent"
android:background="#FFFFFF"
android:layout_gravity="center_horizontal"
android:layout_height="fill_parent">
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
<LinearLayout
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:background="#FFFFFF"
android:layout_gravity="center"
android:layout_height="fill_parent">
<TextView
android:id="@+id/emptytext"
android:paddingTop="100dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#818185"
android:textSize="25sp"
android:textStyle="bold"
android:background="#FFFFFF"
android:text="@string/empty_fav"
android:gravity="center_horizontal|center_vertical"
android:paddingLeft="6dip"
/>
</LinearLayout>
</LinearLayout>
</TableRow>
</TableLayout>
It is the xml for R.layout.custom_list_item_1.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold"
android:background="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
And here is my code.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fav_layout);
....
........
wordDataHelper = new WordDataHelper(getApplicationContext());
favCursor = wordDataHelper.getCursorFav();
startManagingCursor(favCursor);
favAdapter = new SimpleCursorAdapter(
this,
R.layout.custom_list_item_1,
favCursor,
new String[] {WordDataHelper.ENGWORD},
new int[] {android.R.id.text1});
setListAdapter(favAdapter);
list = getListView();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
showVideo(((TextView) view).getText());
}});
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
// @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Context Menu");
menu.add(0, CONTEXT_DELETE, 1, "Delete Item");
}
});
list.setTextFilterEnabled(true);
list.setClickable(true);
}
Now everything is showing fine and all other functionality is also
working fine. But if I am touching the any row, it is not highlighting
i.e. the color is not changing. Is there any problem in my code or
does it have to be implement separately? If so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要设置
ListView 的属性。
检查这个答案Android ListView Selector Color,它解释得很好。
如果这不起作用(我还没有尝试过),请尝试将颜色选择器设置为 R.layout.custom_list_item_1 中定义的 TextView 的背景
You probably need to set the
attribute of the ListView.
Check this answer Android ListView Selector Color, it's well explained.
If that didn't work out (I haven't tried it), try setting a color selector as the background of the TextView defined in R.layout.custom_list_item_1
问题是您将默认的背景可绘制覆盖为纯色,这不支持状态(单击、聚焦等)。如果您希望背景为白色,最好仅使用“浅色”Android 主题之一,例如
@android:style/Theme.Light.NoTitleBar
。The problem is that you're overwriting the default background drawable to a plain color, which doesn't support states (clicked, focused, etc). If you want the background to be white, it might be better to just use one of the "light" Android themes, e.g.
@android:style/Theme.Light.NoTitleBar
.尝试删除 TextView 项目的背景颜色:
Try to remove the background color of your TextView item: