如何在单击时突出显示表格行? [英] How can I highlight the table row on click ?

查看:31
本文介绍了如何在单击时突出显示表格行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As my project requirement i have to highlight the table row on onClick. There is any way to do this? Or please suggest me the alternative?

推荐答案

如果您想像使用通用 ListView 一样使用点击高亮显示的股票,您希望将每一行的背景设置为 android:background="@android:drawable/list_selector_background"

If you want to use the stock on click highlight like you get with a generic ListView, you want to set the background of each row to be 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 中一样获得一个可突出显示的行...

And you should get a highlight-able row just like in a ListView...

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

Above will give you the default theme's list background selector. If you want the more generic selector (like the material design selector when the user touches a row) use this:

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

这不仅适用于 TableRows.您应该能够在几乎所有附加了 onClickListener 的通用小部件(TextViews、Buttons 等)上执行此操作.

Also this applies to more than just TableRows. You should be able to do this on almost any generic widget with an onClickListener attached (TextViews, Buttons, etc).

这篇关于如何在单击时突出显示表格行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆