我在 xml 中有一个 ScrollView,其中包括一个 TableLayout。
我的问题是,每次我单击它时是否可以有一个可聚焦的行。
这是我的 xml 代码:
<ScrollView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/table2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow>
<RelativeLayout
android:id="@+id/rowlayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/rowbackground2" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_code_contact"
android:padding="7dip"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/contacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Contacts"
android:textColor="#000"
android:textSize="18dip"
android:layout_toRightOf="@id/icon"
android:paddingTop="10dip" />
</RelativeLayout>
</TableRow>
<TableRow>
<Button
android:id="@+id/contacts_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/contact_button" />
</TableRow>
我已经尝试过“focusable =“true””和“focusableInTouchMode =“true””但什么也没发生..
提前致谢
如果您询问如何使表格表现得像 ListView,我做了以下操作:
在定义 Activity 布局的 xml 文件中,我定义了该表:
<ScrollView
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<TableLayout
android:id="@+id/my_table"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:stretchColumns="1"
android:shrinkColumns="1"/>
</ScrollView>
然后我就单独写了table_row_item.xml
具有表行布局的文件:
<?xml version="1.0" encoding="UTF-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:focusable="true"
android:focusableInTouchMode="false"
android:clickable="true"
android:background="@android:drawable/list_selector_background">
<TextView
android:id="@+id/cell_1"
...
/>
...
<TextView
android:id="@+id/cell_N"
...
/>
</TableRow>
在活动中,我声明了向表中添加一行的过程:
private void addTableRow() {
final TableLayout table = (TableLayout) findViewById(R.id.my_table);
final TableRow tr = (TableRow) getLayoutInflater().inflate(R.layout.table_row_item, null);
TextView tv;
// Fill out our cells
tv = (TextView) tr.findViewById(R.id.cell_1);
tv.setText(...);
...
tv = (TextView) tr.findViewById(R.id.cell_N);
tv.setText(...);
table.addView(tr);
// Draw separator
tv = new TextView(this);
tv.setBackgroundColor(Color.parseColor("#80808080"));
tv.setHeight(/*height of separaor line. 2dip will be enough*/);
table.addView(tv);
// If you use context menu it should be registered for each table row
registerForContextMenu(tr);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)