Android 以编程方式设置 TextView 的布局权重
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3224193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set the layout weight of a TextView programmatically
提问by eugene
I'm trying to dynamically create TableRow
objects and add them to a TableLayout
.
The TableRow
objects has 2 items, a TextView
and a CheckBox
. The TextView
items need to have their layout weight set to 1 to push the CheckBox
items to the far right.
我正在尝试动态创建TableRow
对象并将它们添加到TableLayout
. 该TableRow
对象有2个项目,一个TextView
和CheckBox
。该TextView
项目需要有自己的布局权重设置为1,推动CheckBox
项目的最右边。
I can't find documentation on how to programmatically set the layout weight of a TextView
item.
我找不到有关如何以编程方式设置TextView
项目布局权重的文档。
回答by Macarse
You have to use TableLayout.LayoutParams
with something like this:
你必须使用TableLayout.LayoutParams
这样的东西:
TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
The last parameter is the weight.
最后一个参数是权重。
回答by Dorje
The answer is that you have to use TableRow.LayoutParams, not LinearLayout.LayoutParams or any other LayoutParams.
答案是您必须使用 TableRow.LayoutParams,而不是 LinearLayout.LayoutParams 或任何其他 LayoutParams。
TextView tv = new TextView(v.getContext());
LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
tv.setLayoutParams(params);
The different LayoutParams are not interchangeable and if you use the wrong one then nothing seems to happen. The text view's parent is a table row, hence:
不同的 LayoutParams 不可互换,如果您使用错误的一个,那么似乎什么都不会发生。文本视图的父级是一个表格行,因此:
http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html
http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html
回答by sberezin
In the earlier answers weightis passed to the constructor of a new SomeLayoutType.LayoutParams object. Still in many cases it's more convenient to use existing objects - it helps to avoid dealing with parameters we are not interested in.
在前面的答案中,权重被传递给新的 SomeLayoutType.LayoutParams 对象的构造函数。在许多情况下,使用现有对象更方便——它有助于避免处理我们不感兴趣的参数。
An example:
一个例子:
// 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);
回答by sberezin
TextView txtview = new TextView(v.getContext());
LayoutParams params = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
txtview.setLayoutParams(params);
1f is denotes as weight=1; you can give 2f or 3f, views will move accoding to the space
1f表示为权重=1;你可以给 2f 或 3f,视图会根据空间移动
回答by Manikandan
just set layout params in that layout like
只需在该布局中设置布局参数,例如
create param variable
创建参数变量
android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
1f is weight variable
1f 是权重变量
set your widget or layout like
设置您的小部件或布局,例如
TextView text = (TextView) findViewById(R.id.text);
text.setLayoutParams(params);
回答by anand krish
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 weightsumfor "LinearLayout".
1f被称为权重=1;根据您的需要,您可以提供 2f 或 3f,视图将根据空间移动。为了使视图之间规定的距离在直线布局使用weightsum为“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);
回答by Ashok Reddy M
This should works to you
这应该对你有用
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT LayoutParams.MATCH_PARENT);
param.weight=1.0f;
回答by kiran boghra
You can also give weight separately like this ,
你也可以像这样单独给出重量,
LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
lp1.weight=1;
回答by Najem1234
This work for me, and I hope it will work for you also
这对我有用,我希望它也对你有用
Set the LayoutParams for the parent view first:
首先为父视图设置 LayoutParams:
myTableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.FILL_PARENT));
then set for the TextView (child):
然后为 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);
回答by Dmitry
There is another way to do this. In case you need to set only one parameter, for example 'height':
还有另一种方法可以做到这一点。如果您只需要设置一个参数,例如“高度”:
TextView textView = (TextView)findViewById(R.id.text_view);
ViewGroup.LayoutParams params = textView.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
textView.setLayoutParams(params);