【发布时间】:2016-05-18 17:35:36
【问题描述】:
我有一个定义了 5 列的表格布局。但是由于第二列中的大文本,内容正在离开屏幕。所以我试图放置一个水平滚动条。却无能为力。我有一个以 HorizontalScrollView 作为父级的 TableLayout。布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Balance_Inquiry" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal" >
<HorizontalScrollView
android:id="@+id/horizontalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:scrollbars="horizontal|vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TableLayout
android:id="@+id/table_layout_balance_inquiry"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:stretchColumns="0,1,2,3,4,5" >
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</LinearLayout>
现在我将数据添加到此表格布局中,如下所示:
private void addHeader() {
// TODO Auto-generated method stub
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(getResources().getColor(R.color.bg_login));
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView labelSrNo = new TextView(this);
labelSrNo.setId(20);
labelSrNo.setText("SL NO.");
labelSrNo.setTextColor(Color.WHITE);
labelSrNo.setPadding(5, 5, 5, 5);
tr_head.addView(labelSrNo);// add the column to the table row here
TextView labelACCNO = new TextView(this);
labelACCNO.setId(21);// define id that must be unique
labelACCNO.setText("ACC NO."); // set the text for the header
labelACCNO.setTextColor(Color.WHITE); // set the color
labelACCNO.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(labelACCNO); // add the column to the table row here
TextView labelACCTYPE = new TextView(this);
labelACCTYPE.setId(20);
labelACCTYPE.setText("ACC TYPE");
labelACCTYPE.setTextColor(Color.WHITE);
labelACCTYPE.setPadding(5, 5, 5, 5);
tr_head.addView(labelACCTYPE);// add the column to the table row here
TextView labelAVAILBLNCE = new TextView(this);
labelAVAILBLNCE.setId(21);// define id that must be unique
labelAVAILBLNCE.setText("AVAIL BALANCE"); // set the text for the header
labelAVAILBLNCE.setTextColor(Color.WHITE); // set the color
labelAVAILBLNCE.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(labelAVAILBLNCE); // add the column to the table row here
TextView extraBalance = new TextView(this);
extraBalance.setId(21);// define id that must be unique
extraBalance.setText("AVAIL BALANCE"); // set the text for the header
extraBalance.setTextColor(Color.WHITE); // set the color
extraBalance.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(extraBalance);
table_layout_balance_inquiry.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
但是水平滚动不起作用。如何启用水平滚动?任何建议都有很大帮助。
【问题讨论】: