在 TableLayout 中以编程方式分配行中的列权重

发布于 2024-12-03 14:50:43 字数 2863 浏览 0 评论 0原文

我只想在表中定义一行。对于这一行,我想添加 3 列,其宽度如下:

textView1 :应该与其内容一样宽

viewDivider:应该是 2px 宽

TextView2:应该占据 tablerow 的剩余区域。

但是,我无法以编程方式实现上述布局。这是代码:

public class Copy_2_of_Test extends Activity {
 TableLayout tableLayout = null;
 TextView textView1 = null;
 TextView textView2 = null;
 View viewDivider = null;

 TableLayout tab = null;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView (R.layout.test);
        tableLayout = (TableLayout)this.findViewById(R.id.mainTable);
        addViews();
      }

      private void addViews() {
       // table row with layout params of type TableLayout.LayoutParams
       TableRow tr = new TableRow(this);
        tr.setLayoutParams(new TableLayout.LayoutParams(
                            LayoutParams.FILL_PARENT, 
                            LayoutParams.FILL_PARENT));

       // Textview 1 with layout params of type TableRow.LayoutParams
        textView1 = new TextView(this);
        textView1.setText("Value1");
        textView1.setTextColor(Color.BLACK);
        textView1.setBackgroundColor(Color.YELLOW);
        textView1.setLayoutParams(new TableRow.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.FILL_PARENT));
        tr.addView(textView1);

        viewDivider = new View (this);
        viewDivider.setLayoutParams(new TableRow.LayoutParams(
                            2,
                            LayoutParams.FILL_PARENT));
        viewDivider.setBackgroundColor(Color.MAGENTA);
        tr.addView(viewDivider);


       // Textview 2 with layout params of type TableRow.LayoutParams
        textView2 = new TextView(this);
        textView2.setText("Value2 Value2  Value2  Value2 ");
        textView2.setTextColor(Color.BLACK);
        textView2.setBackgroundColor(Color.YELLOW);
        textView2.setLayoutParams(new TableRow.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.FILL_PARENT));
        tr.addView(textView2);

        // Add row to TableLayout. 
        tableLayout.addView(tr,new TableLayout.LayoutParams(
             LayoutParams.FILL_PARENT,
             LayoutParams.FILL_PARENT));
   }

    }

这是 test.xml:

     <?xml version="1.0" encoding="utf-8"?>
     <TableLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:stretchColumns="*"
       android:background="@color/black"
       android:id="@+id/mainTable"> 

     </TableLayout>

我面临上述布局的以下问题:

1)textView1 占用的宽度大于其内容。

2)viewDivider占用了很多宽度并且不限制为2px

感谢您的帮助。

I want to define just one row in my table. To this row, I want to add 3 columns, with the following widths:

textView1 : should be as wide as its contents

viewDivider: should be 2px wide

TextView2: should occupy the remaining area of the tablerow.

However, I am not able to achieve the above layout, programatically. Here is the code:

public class Copy_2_of_Test extends Activity {
 TableLayout tableLayout = null;
 TextView textView1 = null;
 TextView textView2 = null;
 View viewDivider = null;

 TableLayout tab = null;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView (R.layout.test);
        tableLayout = (TableLayout)this.findViewById(R.id.mainTable);
        addViews();
      }

      private void addViews() {
       // table row with layout params of type TableLayout.LayoutParams
       TableRow tr = new TableRow(this);
        tr.setLayoutParams(new TableLayout.LayoutParams(
                            LayoutParams.FILL_PARENT, 
                            LayoutParams.FILL_PARENT));

       // Textview 1 with layout params of type TableRow.LayoutParams
        textView1 = new TextView(this);
        textView1.setText("Value1");
        textView1.setTextColor(Color.BLACK);
        textView1.setBackgroundColor(Color.YELLOW);
        textView1.setLayoutParams(new TableRow.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.FILL_PARENT));
        tr.addView(textView1);

        viewDivider = new View (this);
        viewDivider.setLayoutParams(new TableRow.LayoutParams(
                            2,
                            LayoutParams.FILL_PARENT));
        viewDivider.setBackgroundColor(Color.MAGENTA);
        tr.addView(viewDivider);


       // Textview 2 with layout params of type TableRow.LayoutParams
        textView2 = new TextView(this);
        textView2.setText("Value2 Value2  Value2  Value2 ");
        textView2.setTextColor(Color.BLACK);
        textView2.setBackgroundColor(Color.YELLOW);
        textView2.setLayoutParams(new TableRow.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.FILL_PARENT));
        tr.addView(textView2);

        // Add row to TableLayout. 
        tableLayout.addView(tr,new TableLayout.LayoutParams(
             LayoutParams.FILL_PARENT,
             LayoutParams.FILL_PARENT));
   }

    }

And here's test.xml:

     <?xml version="1.0" encoding="utf-8"?>
     <TableLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:stretchColumns="*"
       android:background="@color/black"
       android:id="@+id/mainTable"> 

     </TableLayout>

I am facing thefollowing issues with the above layout:

1) textView1 occupies more width that its contents.

2) viewDivider occupies a lot of width and is not restriced to 2px

Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-12-10 14:50:43

尝试使用 setWidth() 方法设置宽度。

供参考

Try Using setWidth() method for setting width.

For Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文