如何在点击时突出显示表格行?

问题描述 投票:16回答:5

作为我的项目要求,我必须在onClick上突出显示表格行。有办法做到这一点吗?或者请建议我替代方案?

android highlight android-tablelayout
5个回答
25
投票

如果您想使用点击突出显示的股票,就像您使用通用ListView,您想要将每行的背景设置为android:background="@android:drawable/list_selector_background"

这是一个例子:

<TableLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:stretchColumns="0">
  <TableRow
     android:id="@+id/first_row"
     android:background="@android:drawable/list_selector_background" >
    ... row content ...
  </TableRow>
</TableLayout>

然后在代码中,

TableRow firstRow = (TableRow) findViewById(R.id.first_row);
firstRow.setOnClickListener(new OnClickListener() {
       @Override
        public void onClick(View v) {
            // TODO: do your logic here

        }   
}

你应该得到一个高亮表行,就像在ListView中一样...

编辑:上面将为您提供默认主题的列表背景选择器。如果您想要更通用的选择器(如用户触摸行时的材质设计选择器),请使用以下命令:

android:background="?android:attr/selectableItemBackground"

这也不仅仅适用于TableRows。您应该可以在几乎任何附加onClickListener的通用窗口小部件上执行此操作(TextViews,Buttons等)。


11
投票

即使我在salil pandit的帮助下面临同样的问题,但是对它做了一点改动,这对我有用

这是xml中的TableRow

<TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:padding="5dip" 
        android:background="@drawable/selector">

这是selector.xml文件夹中的res\drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item   android:state_focused="true"
            android:state_pressed="true"
            android:drawable="@android:drawable/list_selector_background"></item>
    <item   android:state_focused="true"
            android:state_pressed="false"
            android:drawable="@android:drawable/list_selector_background"></item>
    <item
            android:state_focused="false"
            android:state_pressed="true"
            android:drawable="@android:drawable/list_selector_background" />


     <item android:drawable="@android:drawable/list_selector_background"></item>

</selector>

9
投票

在onclicklistener中添加:

 tr1.setBackgroundResource(drawable.list_selector_background);

哪里tr1是你的桌子。 (你需要让tablerow最终才能工作)。


2
投票
private OnClickListener tablerowOnClickListener = new OnClickListener()
{
    public void onClick(View v)
    {
        //Highlight selected row
        //Highlight selected row
        //Start from 0 to make sure that the first item will also be looped 
        //through
        for (int i = 0; i < tblItemDetail.getChildCount(); i++)
        {
            View row = tblItemDetail.getChildAt(i); 
            if (row == v)
            {
                row.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));               
            }
            else
            {
                //Change this to your normal background color.
                row.setBackgroundColor(getResources().getColor(android.R.color.transparent));
            }
        }
        //...
    }
};

0
投票
String _row_selected = null;
boolean _is_selection_even = false;
private TableLayout TL;
TableRow row_data = new TableRow(this);

row_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (_row_selected != null) {

                    if (Integer.parseInt(_row_selected) == TL.indexOfChild(v)) {

                        if (_is_selection_even) {
                            TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(0xFF00FF00);
                            _is_selection_even = false;
                        } else {
                            TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(Color.WHITE);
                            _is_selection_even = true;
                        }


                    } else {
                        TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(Color.WHITE);
                        v.setBackgroundColor(0xFF00FF00);
                        _row_selected = null;
                        _row_selected = TL.indexOfChild(v) + "";
                    }

                } else {
                    v.setBackgroundColor(0xFF00FF00);
                    _row_selected = null;
                    _row_selected = summaryTL.indexOfChild(v) + "";
                }
              }
        });
© www.soinside.com 2019 - 2024. All rights reserved.