eug*_*ene 207 layout android textview android-layout-weight
我正在尝试动态创建TableRow
对象并将其添加到TableLayout
.该TableRow
对象有2个项目,一个TextView
和CheckBox
.该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)
最后一个参数是重量.
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
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)
你也可以像这样单独给予体重,
LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
lp1.weight=1;
Run Code Online (Sandbox Code Playgroud)
这对你有用
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)
归档时间: |
|
查看次数: |
221207 次 |
最近记录: |