简体   繁体   English

以编程方式将按钮宽度设置为父LinearLayout的50%

[英]Programmatically set button width to 50% of parent LinearLayout

I created a Dialog using LinearLayout and in this Dialog, I have a Button. 我使用LinearLayout创建了一个Dialog,在这个Dialog中,我有一个Button。 I want to set its width to 50% of the Dialog's width, but no matter what I do, the button is still filling the entire width. 我想将其宽度设置为Dialog宽度的50%,但无论我做什么,按钮仍然会填满整个宽度。 The main problem seems to be that I cannot set the button's weight programmatically. 主要问题似乎是我无法以编程方式设置按钮的重量。 I tried creating a second horizontal LinearLayout that would hold the button with weight set to 0.5f (the parent has a weightSum of 1), but that did not change anything. 我尝试创建第二个水平LinearLayout,它将按住权重设置为0.5f的按钮(父亲的weightSum为1),但这并没有改变任何东西。 How can I set the button to be half the width? 如何将按钮设置为宽度的一半?

I've looked at all the other questions but most of them were using xml so they were not relevant, and the one that solved it programmatically was using hardcoded values in pixels which is obviously an incorrect solution to the problem. 我已经查看了所有其他问题,但大多数都使用xml,因此它们不相关,并且以编程方式解决它的是使用像素的硬编码值,这显然是对问题的错误解决方案。

EDIT: Adding some code 编辑:添加一些代码

LinearLayout linearLayout = new LinearLayout(mContext);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setWeightSum(1);

// I use these params when I call addContentView
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

testButton = new Button(mContext);
testButton.setText("Test");
testButton.setId(1);
testButton.setWidth(0);
testButton.setGravity(Gravity.CENTER);

LinearLayout buttonLayout = new LinearLayout(mContext);
butonLayout.setOrientation(LinearLayout.HORIZONTAL);

LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f);
testButton.setLayoutParams(buttonParams);
buttonLayout.addView(testButton);

linearLayout.addView(buttonLayout);

...

this.addContentView(scrollView, layoutParams);

As I tried more and more options, the code became a mess. 当我尝试越来越多的选项时,代码变得一团糟。 I decided to start from scratch and this code worked in the end: 我决定从头开始,这段代码最终起作用:

LinearLayout forButton = new LinearLayout(mContext);
forButton.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams forButtonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
forButton.setGravity(Gravity.CENTER);

DisplayMetrics dm = new DisplayMetrics();
this.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;

forButton.addView(testButton);
testButton.getLayoutParams().width = width/2;

Are you setting the width value to 0dp as well? 您是否也将宽度值设置为0dp? This is required for the layout engine to automatically to use layout weight respectively. 这是布局引擎分别自动使用布局权重所必需的。

Edit 编辑

I think this line will get you the results you are looking for: 我想这一行会为您提供您想要的结果:

LayoutParams buttonParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f);

To find dialog width 要查找对话框宽度

DisplayMetrics dm = new DisplayMetrics();
     dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels ;

To Set Value programmatically 以编程方式设置值

final LayoutParams lparams = new LayoutParams(Height,width/2); // Width , height
    final Button button = new Button(this);
    button.setLayoutParams(lparams);

EDIT 编辑

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 200;
lp.height = 200;
alertDialog.getWindow().setAttributes(lp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM