繁体   English   中英

在桌面android内的焦点行

[英]focusable row inside table android

我在xml中有一个包含一个TableLayout的ScrollView。 我的问题是,如果每次点击它都可以有一个可聚焦的行。 这是我的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,我做了以下:

在xml文件中,我定义了我的Activity的布局:

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

在Activity中我声明了向我的表添加行的过程:

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

您可以使用GridView并将LinearLayout(水平)定义为行。 以此为指导: http//www.learn2crack.com/2014/01/android-custom-gridview.html

如果您正在寻找一种简单有效的解决方案来在表中显示数据,那么TableView将完成这项工作。

SortableTableView示例

当然。 Row具有onClick属性,您可以在其中指定onclick方法的名称。

其他方法是在xml中为此行指定一个id - name。 然后你可以从代码中调用它并添加onClick事件。

此代码将在您选择的可绘制背景(我猜你可以使用null没有背景)和默认Android“选定”背景之间切换。

    row.setClickable(true);
    row.setOnTouchListener(new OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            final int action = event.getAction();
            Log.d("Touched!", "" + action);
            switch (action & MotionEvent.ACTION_MASK)
            {
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
                // case MotionEvent.ACTION_MOVE:
                v.setBackgroundResource(R.drawable.border_bottom);
                v.invalidate();
                break;
            default:
                v
                        .setBackgroundResource(android.R.drawable.list_selector_background);
                v.invalidate();

            }
            return false;
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM