台机器人可聚焦里面排机器人、里面

2023-09-12 06:26:30 作者:奈何桥上等你

我有一个XML格式的滚动型其中包括一个TableLayout。 我的问题是,如果有可能我每次点击它有一个可聚焦一行。 这里是我的XML code:

i have in xml a ScrollView which includes one TableLayout. My question is if it's possible to have a focusable row every time i click on it. Here is my xml code:

    <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>

我已经尝试了可聚焦=真和focusableInTouchMode =真但是什么都没有发生。

I have already tried the "focusable="true"" and "focusableInTouchMode="true"" but nothing happened..

在此先感谢

推荐答案

如果你是问如何让表表现得像ListView中,我做了以下内容:

If you are asking about how to make table to behave like ListView, I did the following:

在XML文件,定义布局我的活动,我定义的表:

In xml-file, which defines layout for my Activity, I defined the table:

<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>

在活动我宣布过程将行添加到我的表:

In Activity I declared procedure to add a row to my table:

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);
}