我希望我的表格中的行在我按下它们时突出显示。但是,我对这个功能的实现导致了以下结果:当我滚动表格时(只是滚动,我不打算按表格行),系统将其解释为 table-row-press 并突出显示该行。仅当我不滚动表格时如何使系统突出显示行?
我制作了示例项目,它由 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>
滚动表格时如何使系统不突出显示该行?