小编use*_*692的帖子

如何设置 tablerows 元素的权重?

我有一个 TableLayout,我以编程方式添加行,我试图以编程方式设置行中包含的元素的“权重”。

我试图通过weightSum在 TableRow 上设置并使用“LayoutParams”的最后一个参数设置列的“权重”来做到这一点,但什么也没出现;相反,如果我给元素一个固定的宽度,代码运行良好。

这是我的布局:

<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>
Run Code Online (Sandbox Code Playgroud)

这是代码:

public class HomeFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.home, null);
    }

    @Override
    public void onStart() {
        super.onStart();

        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(1f);

        TableRow.LayoutParams lp;
        TextView tv1 = new TextView(getActivity());
        lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.6f);
        tv1.setText("AAA");
        tv1.setLayoutParams(lp); …
Run Code Online (Sandbox Code Playgroud)

android android-tablelayout

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

android-tablelayout ×1