219

我正在尝试动态创建TableRow对象并将它们添加到TableLayout. TableRow对象有 2 个项目, a和TextViewa CheckBoxTextView项目需要将其布局权重设置为 1 才能将项目推到CheckBox最右侧。

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

4

12 回答 12

362

你必须使用TableLayout.LayoutParams这样的东西:

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

最后一个参数是重量。

于 2010-07-11T19:33:34.660 回答
99

答案是您必须使用 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 是不可互换的,如果你使用了错误的,那么似乎什么都不会发生。文本视图的父级是表格行,因此:

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

于 2012-07-31T16:06:21.380 回答
40

在前面的答案中,权重被传递给新的 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);
于 2014-05-03T22:52:09.273 回答
17
TextView txtview = new TextView(v.getContext());
LayoutParams params = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
txtview.setLayoutParams(params);

1f 表示为 weight=1;你可以给 2f 或 3f,视图将根据空间移动

于 2016-02-29T07:15:11.397 回答
13

只需在该布局中设置布局参数,例如

创建参数变量

 android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);

1f 是权重变量

设置您的小部件或布局

 TextView text = (TextView) findViewById(R.id.text);
 text.setLayoutParams(params);
于 2013-05-04T09:08:06.037 回答
11
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 is refered as weight=1; according to your need you can give 2f or 3f, views will move accoding to the space. For making specified distance between views in Linear layout use 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);
于 2014-12-08T11:52:57.663 回答
10

这应该对你有用

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT LayoutParams.MATCH_PARENT);

param.weight=1.0f;
于 2016-08-08T11:36:47.833 回答
7

你也可以像这样单独给重量,

LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);

 lp1.weight=1;
于 2014-02-08T10:33:30.490 回答
6

这对我有用,我希望它也对你有用

首先为父视图设置 LayoutParams:

myTableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,
                TableLayout.LayoutParams.FILL_PARENT));

然后为 TextView (child) 设置:

 TableLayout.LayoutParams textViewParam = new TableLayout.LayoutParams
     (TableLayout.LayoutParams.WRAP_CONTENT,
     TableLayout.LayoutParams.WRAP_CONTENT,1f);
     //-- set components margins
     textViewParam.setMargins(5, 0, 5,0);
     myTextView.setLayoutParams(textViewParam); 
于 2012-08-20T23:34:47.233 回答
2

经过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));
于 2019-04-20T15:18:23.473 回答
1

还有另一种方法可以做到这一点。如果您只需要设置一个参数,例如“高度”:

TextView textView = (TextView)findViewById(R.id.text_view);
ViewGroup.LayoutParams params = textView.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
textView.setLayoutParams(params);
于 2014-01-28T19:04:15.940 回答
-1

我在使用与此非常相似的解决方案时遇到了相当大的困难:尝试在 TableRow 中有两个按钮,每个按钮都是屏幕宽度的一半。无论出于何种原因,左侧按钮总是大约为宽度的 70%,右侧按钮为 30%。调用 table_layout.setStretchAllColumns(true) 没有任何效果,也没有将按钮的宽度设置为屏幕的一半,也没有设置它们的布局权重。

我最终得到的解决方案是在 TableRows 中嵌套一个 LinearLayout,它确实考虑了按钮宽度的值。

    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;
        }
于 2011-09-02T20:03:35.653 回答