从代码 android 修改列宽
Modify columns width from code android
我想知道是否有办法修改 table 布局中两列的宽度(总共三列)。
问题是每列的宽度都相同,浪费了很多 space,因为前两个只包含一个 ID 和一个日期,而最后一个包含多行描述。
这是填充 table 布局的代码:
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tr_head.setBackgroundColor(Color.rgb(0, 230, 230));
TextView lblSystemStoricalHeaderId = new TextView(this);
lblSystemStoricalHeaderId.setText("ID ");
tr_head.addView(lblSystemStoricalHeaderId);
TextView lblSystemStoricalHeaderData = new TextView(this);
lblSystemStoricalHeaderData.setText("Data");
tr_head.addView(lblSystemStoricalHeaderData);
TextView lblSystemStoricalHeaderDescription = new TextView(this);
lblSystemStoricalHeaderDescription.setText(" Descrizione");
tr_head.addView(lblSystemStoricalHeaderDescription);
tr_head.setMinimumHeight(30);
tlSystemStorical.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
for(int i=0;i<reportList.length;i++){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
TextView lblSystemStoricalId = new TextView(this);
lblSystemStoricalId.setText(""+reportList[i].getId()+"");
tr.addView(lblSystemStoricalId);
TextView lblSystemStoricalData = new TextView(this);
long dataC = reportList[i].getDate()*1000;
java.util.Date df = new java.util.Date(dataC);
String vv = new SimpleDateFormat("dd/MM/yyyy").format(df);
lblSystemStoricalData.setText(vv);
tr.addView(lblSystemStoricalData);
TableRow.LayoutParams tv3Params = new TableRow.LayoutParams();
tv3Params.width=0;
tv3Params.weight=1;
TextView lblSystemStoricalDescription = new TextView(this);
lblSystemStoricalDescription.setText(""+reportList[i].getReportDescription());
lblSystemStoricalDescription.setLayoutParams(tv3Params);
tr.addView(lblSystemStoricalDescription);
tr.setBackgroundResource(R.drawable.border);
tr.setMinimumHeight(40);
tlSystemStorical.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
这是 xml 文件:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scroll_view_system_description"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="@+id/tl_system_storical"
>
</TableLayout>
</FrameLayout>
</ScrollView>
您可以为 TableRow
children 设置 width
属性 至 0
,但为它们指定 layout_weight
。所以你会写:
float[] weights = new float[] { 0.2f, 0.2f, 0.6f};
TextView lblSystemStoricalId = new TextView(this);
lblSystemStoricalId.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, weights[0]))
其中 0
没有视图宽度(因为您正在使用权重),并且 weights
数组包含三个视图的权重。
这些权重总和应为 1
,每个权重都是列应占宽度的百分比。因此,您需要为 TableRow
children 分配权重 - lblSystemStoricalId
会得到 weights[0]
,lblSystemStoricalData
会得到 weights[1]
和 lblSystemStoricalDescription
会有 weights[2]
.
我想知道是否有办法修改 table 布局中两列的宽度(总共三列)。
问题是每列的宽度都相同,浪费了很多 space,因为前两个只包含一个 ID 和一个日期,而最后一个包含多行描述。
这是填充 table 布局的代码:
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tr_head.setBackgroundColor(Color.rgb(0, 230, 230));
TextView lblSystemStoricalHeaderId = new TextView(this);
lblSystemStoricalHeaderId.setText("ID ");
tr_head.addView(lblSystemStoricalHeaderId);
TextView lblSystemStoricalHeaderData = new TextView(this);
lblSystemStoricalHeaderData.setText("Data");
tr_head.addView(lblSystemStoricalHeaderData);
TextView lblSystemStoricalHeaderDescription = new TextView(this);
lblSystemStoricalHeaderDescription.setText(" Descrizione");
tr_head.addView(lblSystemStoricalHeaderDescription);
tr_head.setMinimumHeight(30);
tlSystemStorical.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
for(int i=0;i<reportList.length;i++){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
TextView lblSystemStoricalId = new TextView(this);
lblSystemStoricalId.setText(""+reportList[i].getId()+"");
tr.addView(lblSystemStoricalId);
TextView lblSystemStoricalData = new TextView(this);
long dataC = reportList[i].getDate()*1000;
java.util.Date df = new java.util.Date(dataC);
String vv = new SimpleDateFormat("dd/MM/yyyy").format(df);
lblSystemStoricalData.setText(vv);
tr.addView(lblSystemStoricalData);
TableRow.LayoutParams tv3Params = new TableRow.LayoutParams();
tv3Params.width=0;
tv3Params.weight=1;
TextView lblSystemStoricalDescription = new TextView(this);
lblSystemStoricalDescription.setText(""+reportList[i].getReportDescription());
lblSystemStoricalDescription.setLayoutParams(tv3Params);
tr.addView(lblSystemStoricalDescription);
tr.setBackgroundResource(R.drawable.border);
tr.setMinimumHeight(40);
tlSystemStorical.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
这是 xml 文件:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scroll_view_system_description"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="@+id/tl_system_storical"
>
</TableLayout>
</FrameLayout>
</ScrollView>
您可以为 TableRow
children 设置 width
属性 至 0
,但为它们指定 layout_weight
。所以你会写:
float[] weights = new float[] { 0.2f, 0.2f, 0.6f};
TextView lblSystemStoricalId = new TextView(this);
lblSystemStoricalId.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, weights[0]))
其中 0
没有视图宽度(因为您正在使用权重),并且 weights
数组包含三个视图的权重。
这些权重总和应为 1
,每个权重都是列应占宽度的百分比。因此,您需要为 TableRow
children 分配权重 - lblSystemStoricalId
会得到 weights[0]
,lblSystemStoricalData
会得到 weights[1]
和 lblSystemStoricalDescription
会有 weights[2]
.