【发布时间】:2012-06-12 05:17:34
【问题描述】:
我有一个搜索Activity,其布局文件如下。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white2" >
<EditText
android:id="@+id/et_search"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:drawableRight="@drawable/search_20"
android:ems="10"
android:hint="Search by name" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey05"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_name"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="0.25"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>
<TextView
android:id="@+id/tv_sno"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Serial No."
android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>
<TextView
android:id="@+id/tv_type"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="0.25"
android:text="Type"
android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>
</LinearLayout>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:transcriptMode="normal" >
</ListView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_chkin"
android:layout_width="0dip"
android:layout_height="50dip"
android:layout_marginBottom="5dip"
android:layout_marginRight="2dip"
android:layout_marginTop="20dip"
android:layout_weight="1"
android:background="@drawable/custombutton"
android:text="Submit" />
<Button
android:id="@+id/btn_chkout"
android:layout_width="0dip"
android:layout_height="50dip"
android:layout_marginBottom="5dip"
android:layout_marginLeft="2dip"
android:layout_marginTop="20dip"
android:layout_weight="1"
android:background="@drawable/custombutton"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
顶部有一个搜索EditText,中间有一个列表视图,屏幕底部有两个按钮。
我无法集中列表视图中的一项。
在列表视图适配器的布局文件中,我设置了android:clickable="false"
android:focusable="false"
仍然当我选择一个项目时,所选项目没有突出显示..
我还尝试将以下代码放入onItemClick
1
// @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
if (lview3 != null) {
lview3.getChildAt(prevPos).setBackgroundColor(Color.BLACK);
lview3.getChildAt(position).setBackgroundColor(Color.BLUE);
}
prevPos = position;
}
2
// @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
lview3.setSelection(position);
}
#1 在模拟器上运行良好,但在我的三星 Galaxy Y 上,它显示 *“应用程序已意外停止。请重试”*
对于#2,只要我触摸屏幕上的项目,项目就会突出显示,一旦我移开手指,突出显示颜色就消失了。
我的 listview_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linLayout_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_name"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Medium Text"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_sno"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Medium Text"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_type"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_weight="0.30"
android:text="Medium Text"
android:textSize="15sp" />
</LinearLayout>
【问题讨论】:
-
android:clickable="false" android:focusable="false" 设置这些属性为真。
-
不要在模拟器中检查你的应用程序,如果你用任何安卓手机检查你可以发现所有的错误。
-
对不起,我还必须为列表视图中的每一行显示我的布局文件。 @Akki:我确实在我之前尝试过的布局文件中将
android:clickable="false" android:focusable="false"设置为true。在这种情况下,它会突出显示所选项目,但只在列表中所选行的上方和下方绘制两条水平线。 -
我觉得可能适配器中用于为列表中的每一行创建
View的TextViews正在消耗点击事件。因此我将可点击和可聚焦设置为false。 -
甚至尝试在
onCreate()和onItemClick()中设置lview3.setItemsCanFocus(true);和lview3.setItemChecked(position, true);
标签: android android-layout android-widget