【问题标题】:Table row is highlighted while scrolling the table滚动表格时突出显示表格行
【发布时间】:2011-12-23 09:37:50
【问题描述】:

我希望表格中的行在我按下它们时突出显示。但是,我对此功能的实现导致以下结果:当我滚动表格时(只是滚动,我不打算按表格行),系统将其解释为表格行并突出显示该行。 不滚动表格时如何使系统突出显示行?

我已经制作了示例项目,它由 3 个文件组成:类定义、XML 布局和 XML 文件,其中指定了按下表格行时要执行的操作。 他们在这里:

类定义

    public class RowHighlightTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TableLayout mainTable=(TableLayout)findViewById(R.id.table);
        Resources r=getResources();
        // We gonna have 25 rows in our table
        for (int i = 0; i < 25; i++) {
            // Set up each row
            TableRow row=new TableRow(this);
            row.setMinimumHeight(60);
            row.setClickable(true);
            row.setBackgroundDrawable(r.getDrawable(R.drawable.table_row));

            // Add a textView to each row to be able to distinguish them
            TextView tv=new TextView(this);
            tv.setText("row " + i);
            row.addView(tv);
            mainTable.addView(row,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        }
    }
}

XML 布局:res/layout/main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="none" >

        <TableLayout
            android:id="@+id/table"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </TableLayout>
    </ScrollView>

</LinearLayout>

第三个,res/drawable/table_row.xml,这里指定表格行默认为白色,按下时为黑色:

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@android:color/background_light" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@android:color/background_dark" android:state_pressed="true"/>

</selector>

滚动表格时如何使系统不突出显示该行?

【问题讨论】:

    标签: android android-tablelayout


    【解决方案1】:

    也许您尝试这样做的方式不正确。

    您为什么不尝试在 ScrollView 中使用 ListView 而不是 Table ?困难的滚动/选择处理已经完成并满足您的需求...

    【讨论】:

    • 我自己找到了一种方法来处理它,但是是的,列表视图会是更好的选择......
    猜你喜欢
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2018-01-07
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多