以编程方式设置TextView的布局权重

eug*_*ene 207 layout android textview android-layout-weight

我正在尝试动态创建TableRow对象并将其添加到TableLayout.该TableRow对象有2个项目,一个TextViewCheckBox.该TextView项目需要有自己的布局权重设置为1,以推动CheckBox项目的最右边.

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

Mac*_*rse 353

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

TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
Run Code Online (Sandbox Code Playgroud)

最后一个参数是重量.

  • 我以前没有提过这个,所以我很抱歉.但我在问我的问题之前尝试了这个.它使TextView从布局中消失.但是,从积极的方面来说,我发现将TableLayout的stretch_columns属性设置为0会导致我正在寻找的效果(TextView位于左侧,CheckBoxes位于右侧).谢谢您的帮助. (15认同)
  • 如果要导入XML布局而不是动态创建布局,可以像这样抓取它:`TextView tv =(TextView)findViewById(R.id.your_textview_inside_the_linearlayout); tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,0.1f));` (8认同)
  • 要以编程方式设置布局权重,应使用`TableRow.LayoutParams`而不是`TableLayout.LayoutParams`.见@ Dorje的回答. (4认同)

Dor*_*rje 96

答案是你必须使用TableRow.LayoutParams,而不是LinearLayout.LayoutParams或任何其他LayoutParams.

TextView tv = new TextView(v.getContext());
LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
tv.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)

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

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

  • 这个答案的关键部分是0 (5认同)

sbe*_*zin 38

在之前的答案中,权重被传递给新的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);
Run Code Online (Sandbox Code Playgroud)


小智 16

TextView txtview = new TextView(v.getContext());
LayoutParams params = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
txtview.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)

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


Man*_*dan 12

只需在布局中设置布局参数

创建param变量

 android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
Run Code Online (Sandbox Code Playgroud)

1f是重量变量

设置你的小部件或布局

 TextView text = (TextView) findViewById(R.id.text);
 text.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)


ana*_*ish 11

TextView text = new TextView(v.getContext());
text.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                                                LayoutParams.WRAP_CONTENT, 1f));
Run Code Online (Sandbox Code Playgroud)

(要么)

TextView tv = new TextView(v.getContext());
LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
tv.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)

1f被称为weight = 1; 根据您的需要,你可以给2f或3f,视图将根据空间移动.要在线性布局中创建视图之间的指定距离,请使用权重 "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);
Run Code Online (Sandbox Code Playgroud)


kir*_*hra 7

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

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

 lp1.weight=1;
Run Code Online (Sandbox Code Playgroud)


Ash*_*y M 7

这对你有用

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

param.weight=1.0f;
Run Code Online (Sandbox Code Playgroud)


小智 6

这项工作对我来说,我希望它也适合你

首先为父视图设置LayoutParams:

myTableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,
                TableLayout.LayoutParams.FILL_PARENT));
Run Code Online (Sandbox Code Playgroud)

然后设置为TextView(子):

 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); 
Run Code Online (Sandbox Code Playgroud)