添加一些代码LinearLayout linearLayout = new LinearLayout(mContext);linearLayout.setOrientation(LinearLayout.VERTICAL);linearLayout.setWeightSum(1);// I use these params when I call addContentViewLinearLayout.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);解决方案随着我尝试越来越多的选项,代码变得一团糟.我决定从头开始,这段代码最终奏效: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;I created a Dialog using LinearLayout and in this Dialog, I have a 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. 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. 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.EDIT: Adding some codeLinearLayout linearLayout = new LinearLayout(mContext);linearLayout.setOrientation(LinearLayout.VERTICAL);linearLayout.setWeightSum(1);// I use these params when I call addContentViewLinearLayout.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; 这篇关于以编程方式将按钮宽度设置为父级LinearLayout的50%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-31 00:31