简体   繁体   English

具有WrapContent宽度的垂直LinearLayout-使子项填充到最宽的子项

[英]Vertical LinearLayout with WrapContent width - make children fill it up to widest child

I want to create vertical LinearLayout with couple of Button children, where each child has width of widest of them. 我想创建几个Button子项的垂直LinearLayout ,其中每个子项的宽度最宽。

However depending on using MATCH_PARENT or WRAP_CONTENT for children width, I get either LinearLayout taking whole screen's width, or Buttons not filling LinearLayout . 但是,根据使用MATCH_PARENTWRAP_CONTENT作为子级宽度,我得到的LinearLayout占据整个屏幕的宽度,或者Buttons不能填充LinearLayout Screenshots below (fill/wrap): 下面的屏幕截图(填充/包装):

填写示例包装示例

Example Activity code: 活动代码示例:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    RelativeLayout mainView = new RelativeLayout(this);
    mainView.setBackgroundColor(Colors.WHITE);

    String[] buttonsNames = new String[] { "Short", "Looooooong", "Medium" };
    View buttonsView = getButtonsView(buttonsNames);

    mainView.addView(buttonsView, new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT));
    setContentView(mainView);
}

private View getButtonsView(String[] buttonNames) {
    LinearLayout buttonsView = new LinearLayout(this);
    buttonsView.setOrientation(LinearLayout.VERTICAL);
    buttonsView.setBackgroundColor(Colors.BLACK);

    for (int i = 0; i < buttonNames.length; i++) {
        Button button = new Button(this);
        button.setText(buttonNames[i]);

        ///////////// HERE LAYS THE PROBLEM //////////

        buttonsView.addView(button, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                //LinearLayout.LayoutParams.WRAP_CONTENT, // neither of them works
                LinearLayout.LayoutParams.WRAP_CONTENT));

        View redLineDivider = new View(this);
        redLineDivider.setBackgroundColor(Colors.RED);
        buttonsView.addView(redLineDivider, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, 2));
    }

    return buttonsView;
}

As you can see on second screenshot, red lines actually take whole width without stretching LinearLayout - it is because at least one view has set width. 如您在第二张屏幕截图中所见,红线实际上占据了整个宽度而没有拉伸LinearLayout这是因为至少一个视图已设置宽度。

Potential fix I have came up with is to find widest button (with longest text) and make it use WRAP_CONTENT , while all the rest use MATCH_PARENT , which gives me expected result: 我想出的潜在解决方法是找到最宽的按钮(具有最长的文本),并使其使用WRAP_CONTENT ,而其余​​所有按钮均使用MATCH_PARENT ,这给了我预期的结果:

想要的结果

Code: 码:

buttonsView.addView(button, new LinearLayout.LayoutParams(
                isLongestText(i) ? LinearLayout.LayoutParams.WRAP_CONTENT
                        : LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));

It doesn't feel like elegant solution though - is there any intended mechanism for situation like this, that I am missing out? 但是,这感觉不像是一种优雅的解决方案-是否有针对这种情况的预期机制,我错过了?

Following is the trick: 以下是技巧:

  1. Mention the width of the LinearLayout containing the buttons (buttonsView in your code) as WRAP_CONTENT. 提及包含按钮(代码中的buttonsView)的LinearLayout的宽度为WRAP_CONTENT。
  2. Mention the width of each button as MATCH_PARENT 将每个按钮的宽度指定为MATCH_PARENT

Your program should give you the expected result if you do not include the redLineDivider View. 如果您不包括redLineDivider视图,您的程序应会给您预期的结果。 There seems to be some issue with setting the width of redLineDivider. 设置redLineDivider的宽度似乎存在一些问题。 As an alternative you can declare it as a LinearLayout to make your code work perfectly. 或者,您可以将其声明为LinearLayout,以使您的代码完美地工作。

// View redLineDivider = new View(this);
// Instead declare it as a LinearLayout
LinearLayout redLineDivider = new LinearLayout(this);

Hope this will be useful. 希望这会有用。

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

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