【问题标题】:Programatically assign column weight in a row, within TableLayout在 TableLayout 中以编程方式在一行中分配列权重
【发布时间】:2011-09-10 12:08:43
【问题描述】:

我只想在表格中定义一行。对于这一行,我想添加 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

感谢您的帮助。

【问题讨论】:

    标签: android android-layout tablelayout


    【解决方案1】:

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

    For Reference

    【讨论】:

      猜你喜欢
      • 2011-02-13
      • 1970-01-01
      • 2012-03-29
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 2013-04-29
      相关资源
      最近更新 更多