android – 在TableRow下设置TextView的layout_weight
作者:互联网
这个问题实际上与这篇文章Set the layout weight of a TextView programmatically有关
根据答案我只需要设置TextView布局参数如下并设置stretchColumn属性,但通过添加以下代码,它使textView从表格布局中消失.
TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
所以,这是我的代码,我想动态添加一行3列,权重为35%,5%和60%.请告诉我我的代码有什么问题.
private void addTableRow(int resIdTitle, String strValue){
TableRow tr = new TableRow(this);
// Add First Column
TextView tvTitle = new TextView(this);
tvTitle.setText(resIdTitle);
tvTitle.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.35f));
tr.addView(tvTitle);
// Add 2nd Column
TextView tvColon = new TextView(this);
tvColon.setText(" : ");
tvColon.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.05f));
tr.addView(tvColon);
// Add the 3rd Column
TextView tvValue = new TextView(this);
tvValue.setText(strValue);
tvValue.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.60f));
tr.addView(tvValue);
// Finally add it to the table
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
Log.d(TAG, "Row Added");
}
这是我的xml文件,
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableLayout
android:id="@+id/tl_cam_get_param"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:title="@string/strGetParamTitle"
android:scrollbars="vertical"
android:shrinkColumns="0">
</TableLayout>
</ScrollView>
我也尝试将layout_width设置为0,但它仍然无法正常工作.
谢谢,
artsylar
解决方法:
谢谢大家!
最后,我能够通过参考以下帖子来解决我的问题.
android: two issues using Tablerow+TextView in Tablelayout
How to make a TableLayout from XML using programmable way ?
请参阅下面的工作代码.
这是动态添加行的地方.
TableLayout tl = (TableLayout)findViewById(R.id.table_layout);
private void addTableRow(int resIdTitle, String strValue){
LayoutInflater inflater = getLayoutInflater();
TableRow tr = (TableRow)inflater.inflate(R.layout.table_row, tl, false);
// Add First Column
TextView tvTitle = (TextView)tr.findViewById(R.id.tvTitle);
tvTitle.setText(resIdTitle);
// Add the 3rd Column
TextView tvValue = (TextView)tr.findViewById(R.id.tvValue);
tvValue.setText(strValue);
tl.addView(tr);
}
这是table_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableLayout
android:id="@+id/table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:title="@string/strGetParamTitle"
android:scrollbars="vertical"
android:shrinkColumns="0">
</TableLayout>
</ScrollView>
最后是table_row.xml.我在这里设置了所有layout_params.
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/tvTitle"
android:paddingRight="3dip"
android:layout_width="0px"
android:layout_weight="0.35"/>
<TextView android:text=":"
android:layout_width="0px"
android:layout_weight="0.05"/>
<TextView
android:id="@+id/tvValue"
android:paddingLeft="3dip"
android:layout_width="0px"
android:layout_weight="0.6"
/>
</TableRow>
标签:tablelayout,android,textview 来源: https://codeday.me/bug/20191006/1863426.html