android 动态 设置layout_weight,android - 如何从代码中动态设置layout_weight属性?
android - 如何從代碼中動態(tài)設(shè)置layout_weight屬性?
如何從java代碼動態(tài)設(shè)置android中的按鈕屬性layout_weight的值?
9個解決方案
377 votes
您可以將其作為LinearLayout.LayoutParams構(gòu)造函數(shù)的一部分傳遞:
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f
);
YOUR_VIEW.setLayoutParams(param);
最后一個參數(shù)是重量。
Erich Douglass answered 2019-03-16T13:48:22Z
108 votes
使用LinearLayout.LayoutParams:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);
編輯:啊,Erich的答案更容易!
kcoppock answered 2019-03-16T13:48:54Z
74 votes
如果您已經(jīng)在layout(xml)文件中定義了視圖,只想以編程方式更改權(quán)重,這種方式更好
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
mButton.getLayoutParams();
params.weight = 1.0f;
mButton.setLayoutParams(params);
new a LayoutParams會覆蓋您在xml文件中定義的其他參數(shù),如margin,或者您需要在LayoutParams中指定所有這些參數(shù)。
pkliang answered 2019-03-16T13:49:28Z
19 votes
如果寬度,高度和重量的構(gòu)造函數(shù)不起作用,請嘗試使用寬度和高度的構(gòu)造函數(shù)。 然后手動設(shè)置重量。
如果要根據(jù)權(quán)重設(shè)置寬度,請在構(gòu)造函數(shù)中將width設(shè)置為0。 同樣適用于身高。下面的代碼適合我。
LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT);
childParam1.weight = 0.3f;
child1.setLayoutParams(childParam1);
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT);
childParam1.weight = 0.7f;
child2.setLayoutParams(childParam2);
parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);
Ganindu answered 2019-03-16T13:50:02Z
12 votes
如果我有人在尋找答案,請使用:
LinearLayout.LayoutParams lay = (LinearLayout.LayoutParams) myLayout.getLayoutParams();
lay.weight = 0.5;
如果要從xml文件初始化布局,這比為線性布局提供新的布局參數(shù)要方便得多。
ash1103 answered 2019-03-16T13:50:35Z
6 votes
任何MATCH_PARENT和WRAP_CONTENT為我工作,對于按鈕右邊的是TableRow.LayoutParams.這是:
TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1f);
關(guān)于使用MATCH_PARENT或WRAP_CONTENT是一樣的。
gantzer89 answered 2019-03-16T13:51:10Z
2 votes
如果您已經(jīng)在布局(xml)文件中定義了視圖并且只想以編程方式更改權(quán)重,那么創(chuàng)建新的LayoutParams將覆蓋您在xml文件中定義的其他參數(shù)。
首先你應(yīng)該使用“getLayoutParams”然后使用setLayoutParams
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mButton.getLayoutParams();
params.weight = 4f;
mButton.setLayoutParams(params);
Shirish Herwade answered 2019-03-16T13:51:51Z
1 votes
如果已經(jīng)定義了layoutparams(以XML或動態(tài)方式),這是一個單行:
((LinearLayout.LayoutParams) mView.getLayoutParams()).weight = 1;
SolidSnake answered 2019-03-16T13:52:20Z
0 votes
使用Kotlin你可以做類似的事情:
import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v7.widget.CardView
import android.widget.*
import android.widget.LinearLayout
class RespondTo : CardView {
constructor(context: Context) : super(context) {
init(context)
}
private fun init(context: Context) {
val parent = LinearLayout(context)
parent.apply {
layoutParams = LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT, 1.0f).apply {
orientation = LinearLayout.HORIZONTAL
addView(EditText(context).apply {
id = generateViewId()
layoutParams = LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 0.9f).apply {
}
})
addView(ImageButton(context).apply({
layoutParams = LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 0.1f)
background = null
setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_save_black_24px))
id = generateViewId()
layoutParams = RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT).apply {
addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
// addRule(RelativeLayout.LEFT_OF, myImageButton.id)
}
}))
}
}
this.addView(parent)
}
}
Hasan A Yousef answered 2019-03-16T13:52:49Z
總結(jié)
以上是生活随笔為你收集整理的android 动态 设置layout_weight,android - 如何从代码中动态设置layout_weight属性?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腹式呼吸如何锻炼
- 下一篇: jq实现表格冻结窗格的样式