我正在尝试动态创建TableRow对象,并将它们添加到tableelayout。 TableRow对象有两个项,一个TextView和一个CheckBox。TextView项需要将其布局权重设置为1,以将CheckBox项推到最右边。

我找不到关于如何以编程方式设置TextView项的布局权重的文档。


当前回答

在前面的回答中,权重被传递给一个新的SomeLayoutType的构造函数。LayoutParams对象。 在许多情况下,使用现有对象更方便——这有助于避免处理我们不感兴趣的参数。

一个例子:

// Get our View (TextView or anything) object:
View v = findViewById(R.id.our_view); 

// Get params:
LinearLayout.LayoutParams loparams = (LinearLayout.LayoutParams) v.getLayoutParams();

// Set only target params:
loparams.height = 0;
loparams.weight = 1;
v.setLayoutParams(loparams);

其他回答

经过4个小时的挣扎。 最后,这段代码为我工作了。

3列是一排的。

  TextView serialno = new TextView(UsersActivity.this);
  TextView userId = new TextView(UsersActivity.this);
  TextView name = new TextView(UsersActivity.this);

  serialno.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
  userId.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
  name.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));

在前面的回答中,权重被传递给一个新的SomeLayoutType的构造函数。LayoutParams对象。 在许多情况下,使用现有对象更方便——这有助于避免处理我们不感兴趣的参数。

一个例子:

// Get our View (TextView or anything) object:
View v = findViewById(R.id.our_view); 

// Get params:
LinearLayout.LayoutParams loparams = (LinearLayout.LayoutParams) v.getLayoutParams();

// Set only target params:
loparams.height = 0;
loparams.weight = 1;
v.setLayoutParams(loparams);

我遇到了相当大的困难,解决方案非常类似于这个:尝试在TableRow中有两个按钮,每个按钮是屏幕宽度的一半。不管出于什么原因,左键总是在宽度的70%左右,右键是30%。调用table_layout.setStretchAllColumns(true)没有效果,将按钮的宽度设置为屏幕的一半也没有效果,设置它们的布局权重也没有效果。

我最终的解决方案是在TableRows中嵌套线性布局,这确实考虑了按钮宽度的值。

    TableLayout layout = new TableLayout(this);
    TableRow top_row = new TableRow(this);
    left_button = styleButton();
    right_button = styleButton();
    LinearLayout toprow_layout = new LinearLayout (this);
    toprow_layout.setOrientation(LinearLayout.HORIZONTAL);
    toprow_layout.addView (left_button);
    toprow_layout.addView(right_button);
    toprow.addView(top_layout);
    layout.addView(top_row)

    private Button styleButton() {
            Button btn = new Button (this);
            android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();      
            btn.setWidth((int)(display.getWidth()/2));    // set width to half
            btn.setHeight(((int)display.getHeight()/6));  // set height to whatevs
            btn.setText("foo");
            return btn;
        }
TextView text = new TextView(v.getContext());
text.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                                                LayoutParams.WRAP_CONTENT, 1f));

(OR)

TextView tv = new TextView(v.getContext());
LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
tv.setLayoutParams(params);

1f表示weight=1;根据您的需要,您可以给出2f或3f,视图将根据空间移动。为了在线性布局中使视图之间的指定距离使用weightsum For“LinearLayout”。

LinearLayout ll_Outer= (LinearLayout ) view.findViewById(R.id.linearview);
LinearLayout llInner = new LinearLayout(this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);
            llInner.Orientation = Orientation.Horizontal;
            llInner.WeightSum = 2;
            ll_Outer.AddView(llInner);

答案是您必须使用TableRow。LayoutParams,不是LinearLayout。LayoutParams或任何其他LayoutParams。

TextView tv = new TextView(v.getContext());
LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
tv.setLayoutParams(params);

不同的LayoutParams是不可互换的,如果你使用了错误的LayoutParams,那么似乎什么都不会发生。文本视图的父视图是一个表行,因此:

http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html