Android 如何设置tablerows元素的权重?
我有一个以编程方式添加行的TableLayout,我试图以编程方式设置行中包含的元素的“权重” 我试图通过在TableRow上设置Android 如何设置tablerows元素的权重?,android,android-tablelayout,Android,Android Tablelayout,我有一个以编程方式添加行的TableLayout,我试图以编程方式设置行中包含的元素的“权重” 我试图通过在TableRow上设置weightSum,并使用最后一个参数“LayoutParams”设置列的“weight”,来实现这一点,但什么也没有出现;相反,如果我给元素一个固定的宽度,代码就可以正常工作 这是我的布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:
weightSum
,并使用最后一个参数“LayoutParams”设置列的“weight”,来实现这一点,但什么也没有出现;相反,如果我给元素一个固定的宽度,代码就可以正常工作
这是我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/sample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center" >
</TableLayout>
</LinearLayout>
下面是以编程方式为tableLayout设置权重和列权重的更新代码
TableLayout tl = (TableLayout)getActivity().findViewById(R.id.sample);
TableRow tr = new TableRow(getActivity());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.setGravity(Gravity.CENTER);
tr.setWeightSum(4); //total row weight
TableRow.LayoutParams lp;
lp.weight = 1; //column weight
TextView tv1 = new TextView(getActivity());
lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.6f);
tv1.setText("AAA");
tv1.setLayoutParams(lp);
TextView tv2 = new TextView(getActivity());
lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.4f);
tv2.setText("BBB");
tv2.setLayoutParams(lp);
tr.addView(tv1);
tr.addView(tv2);
tl.addView(tr);
要设置TableRow的LayoutParams,请使用TableLayout.LayoutParams..是的,现在它工作了,我不知道为什么我没有看到它。。。非常感谢。
TableLayout tl = (TableLayout)getActivity().findViewById(R.id.sample);
TableRow tr = new TableRow(getActivity());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.setGravity(Gravity.CENTER);
tr.setWeightSum(4); //total row weight
TableRow.LayoutParams lp;
lp.weight = 1; //column weight
TextView tv1 = new TextView(getActivity());
lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.6f);
tv1.setText("AAA");
tv1.setLayoutParams(lp);
TextView tv2 = new TextView(getActivity());
lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.4f);
tv2.setText("BBB");
tv2.setLayoutParams(lp);
tr.addView(tv1);
tr.addView(tv2);
tl.addView(tr);