如何从代码动态设置layout_weight属性?

发布于 2024-10-11 17:52:36 字数 67 浏览 7 评论 0原文

如何从 java 代码动态设置 android 中按钮的属性 layout_weight 值?

How can I set the value for the attribute layout_weight for button in android dynamically from java code ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

萧瑟寒风 2024-10-18 17:52:36

您可以将其作为 LinearLayout.LayoutParams 构造函数的一部分传递:

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
    LayoutParams.MATCH_PARENT,
    LayoutParams.MATCH_PARENT,
    1.0f
);
YOUR_VIEW.setLayoutParams(param);

最后一个参数是权重。

You can pass it in as part of the LinearLayout.LayoutParams constructor:

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
    LayoutParams.MATCH_PARENT,
    LayoutParams.MATCH_PARENT,
    1.0f
);
YOUR_VIEW.setLayoutParams(param);

The last parameter is the weight.

彩扇题诗 2024-10-18 17:52:36

使用 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 的回答更简单!

Use 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);

EDIT: Ah, Erich's answer is easier!

等风来 2024-10-18 17:52:36

如果您已经在布局(xml)文件中定义了视图,只想以编程方式更改权重,这种方式更好,

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)   
mButton.getLayoutParams();
params.weight = 1.0f;
mButton.setLayoutParams(params);

新的 LayoutParams 会覆盖 xml 文件中定义的其他参数(例如边距),或者您需要在 LayoutParams 中指定所有参数。

If you already define your view in your layout(xml) file, only want to change the weight programmatically, this way is better

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)   
mButton.getLayoutParams();
params.weight = 1.0f;
mButton.setLayoutParams(params);

new a LayoutParams overwrites other params defined in you xml file like margins, or you need to specify all of them in LayoutParams.

把回忆走一遍 2024-10-18 17:52:36

如果具有宽度、高度和重量的构造函数不起作用,请尝试使用具有宽度和高度的构造函数。然后手动设置重量。

如果希望根据权重设置宽度,请在构造函数中将宽度设置为 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);
childParam2.weight = 0.7f;
child2.setLayoutParams(childParam2);

parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);

If the constructor with width, height and weight is not working, try using the constructor with width and height. And then manually set the weight.

And if you want the width to be set according to the weight, set width as 0 in the constructor. Same applies for height.
Below code works for me.

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);
childParam2.weight = 0.7f;
child2.setLayoutParams(childParam2);

parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);
霓裳挽歌倾城醉 2024-10-18 17:52:36

如果 layoutparams 已经定义(在 XML 中或动态定义),下面是一个行:

((LinearLayout.LayoutParams) mView.getLayoutParams()).weight = 1;

If layoutparams is already defined (in XML or dynamically), Here's a one liner:

((LinearLayout.LayoutParams) mView.getLayoutParams()).weight = 1;
小情绪 2024-10-18 17:52:36

如果我有人寻找答案,请使用这个:

LinearLayout.LayoutParams lay = (LinearLayout.LayoutParams) myLayout.getLayoutParams();
lay.weight = 0.5;

如果您从 xml 文件初始化布局,这将比为线性布局提供新的布局参数方便得多。

If I someone looking for answer, use this:

LinearLayout.LayoutParams lay = (LinearLayout.LayoutParams) myLayout.getLayoutParams();
lay.weight = 0.5;

If you are initializing your layout from xml file, this will be much more convenient than providing new layout parameters for Linear Layout.

梦魇绽荼蘼 2024-10-18 17:52:36

LinearLayout.LayoutParamsTableLayout.LayoutParams 中的任何一个都对我有用,对于按钮,正确的一个是 TableRow.LayoutParams。即:

            TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT, 1f);

关于使用 MATCH_PARENTWRAP_CONTENT 是相同的。

Any of LinearLayout.LayoutParams and TableLayout.LayoutParams worked for me, for buttons the right one is TableRow.LayoutParams. That is:

            TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT, 1f);

About using MATCH_PARENT or WRAP_CONTENT be the same.

中二柚 2024-10-18 17:52:36

如果您已经在layout(xml) 文件中定义了视图,并且只想在语法上更改权重程序,那么创建新的LayoutParams 会覆盖您在xml 文件中定义的其他参数。

所以首先你应该使用“getLayoutParams”,然后setLayoutParams

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mButton.getLayoutParams();
params.weight = 4f;
mButton.setLayoutParams(params);

If you have already defined your view in layout(xml) file and only want to change the weight pro grammatically, then then creating new LayoutParams overwrites other params defined in you xml file.

So first you should use "getLayoutParams" and then setLayoutParams

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mButton.getLayoutParams();
params.weight = 4f;
mButton.setLayoutParams(params);

指尖上的星空 2024-10-18 17:52:36

我通过添加这行代码完成了我的任务!

LinearLayout.LayoutParams mylayout = (LinearLayout.LayoutParams)   
myview.getLayoutParams();
mylayout.weight = 2;
myview.setLayoutParams(mylayout);

I have done my task by adding this line of code!

LinearLayout.LayoutParams mylayout = (LinearLayout.LayoutParams)   
myview.getLayoutParams();
mylayout.weight = 2;
myview.setLayoutParams(mylayout);
情仇皆在手 2024-10-18 17:52:36

使用 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)
    }
}

Using Kotlin you can do something like:

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)
    }
}
夏至、离别 2024-10-18 17:52:36

如果您想将权重设置为 LinearLayout ,请使用下面提到的代码或将权重设置为relative layout,然后将其替换为relative layout

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f

);
YOUR_VIEW.setLayoutParams(param);

if you want to set weight to LinearLayout then use code mentioned below or set weight to Relative Layout then replace it with Relative Layout

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f

);
YOUR_VIEW.setLayoutParams(param);

不弃不离 2024-10-18 17:52:36

你可以尝试这个方法。
首先,您获取与此 Button 关联的 LayoutParams。执行类型转换。

LinearLayout.LayoutParams linearLayoutParams = (LinearLayout.LayoutParams) button.getLayoutParams();

然后为“权重”参数分配所需的值。

linearLayoutParams.weight = 5;

重新绘制布局。

button.requestLayout();

You can try this approach.
First, you get the LayoutParams associated with this Button. Perform a type conversion.

LinearLayout.LayoutParams linearLayoutParams = (LinearLayout.LayoutParams) button.getLayoutParams();

Then assign the required value of the "weight" parameter.

linearLayoutParams.weight = 5;

Redraw the layout.

button.requestLayout();
世俗缘 2024-10-18 17:52:36

在 Kotlin 中您可以这样做:

val params = mButton.layoutParams as LinearLayout.LayoutParams
params.weight = 1.0f
mButton.layoutParams = params

This is how you do it in Kotlin:

val params = mButton.layoutParams as LinearLayout.LayoutParams
params.weight = 1.0f
mButton.layoutParams = params
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文