android - 如何设置tablerows元素的权重?

标签 android android-tablelayout

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

我试图通过在 TableRow 上设置 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>

这是代码:

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);
        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);
    }
}

最佳答案

以下是更新后的代码,用于以编程方式为 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);

关于android - 如何设置tablerows元素的权重?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16751948/

相关文章:

android - 在一行中对齐和调整textview和edittext的宽度

java - 适用于 Android、iOS 和黑莓的 Codenameone 应用内计费

android-layout - Android RatingBar 大小

Android:将布局膨胀为 fragment 但 onCreate 不启动

java - Android表格布局从手机边缘开始

android - 问号而不是 RTL 字段

java - 如何将上下文菜单添加到表格布局?

android - TableRow 的列改变了它的大小

android - 如何将 android 应用程序移植到黑莓?

android - 使用 ant 制作 classes.dex 时出错