Android程序中如何通过编程方式在布局中对一组按钮进行对齐

3
我想在我的应用程序中实现的事情是动态创建一组具有不同文本大小的按钮,并将这些视图对齐在线性布局内。 这是我想要实现的示例:image。根据文本大小,我希望按钮在单行中对齐,如果最后一个按钮大于屏幕大小,则应正确进入下一行。 有什么建议如何实现这一点或者我应该寻找什么,因为我知道如果我尝试button.getWidth();它会返回0并且不起作用。 感谢任何建议!
编辑
为了实现这一点,您可以使用Google开源的新布局FlexboxLayout。 您可以在Android Developers Blog的post中了解更多信息。

尝试使用自定义的水平ListView。 - jimpanzer
可以看一下http://www.androidviews.net/2012/11/chips-text-fields/,也许会有所帮助。同时我也在阅读它的代码。 - Naveen
你能提供上面显示的屏幕截图的代码吗?如果有,我正在寻找基于文本宽度的动态按钮解决方案。 - Venkatesh
@VenkateshNani 我认为这个对你有用:https://android-developers.googleblog.com/2017/02/build-flexible-layouts-with.html - hardartcore
4个回答

2
感谢@Kameswari的代码给了我正确的方向,我通过使用以下方法实现了这个功能:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);

String[] mKeywordsArray = mKeywords.split(", ");
if(mKeywordsArray != null){

    LinearLayout mNewLayout = new LinearLayout(getActivity()); // Horizontal layout which I am using to add my buttons.
    mNewLayout.setOrientation(LinearLayout.HORIZONTAL);
    int mButtonsSize = 0;
    Rect bounds = new Rect();

    for(int i = 0; i < mKeywordsArray.length; i++){

        String mButtonTitle = mKeywordsArray[i];
        Button mBtn = new Button(getActivity());
        mBtn.setPadding(10, 3, 10, 3);
        mBtn.setText(mButtonTitle);

        Paint textPaint = mBtn.getPaint();
        textPaint.getTextBounds(mButtonTitle, 0, mButtonTitle.length(), bounds);
            // size of all buttons in current horizontal layout
            // i am using +45 because of extra padding which is set in xml for this layout
        mButtonsSize += ( bounds.width() + 45);
        if(mButtonsSize < (mScreenWidth - 32)){ // -32 because of extra padding in main layout.
            mNewLayout.addView(mBtn, params);
        } else {
            mButtonsLayout.addView(mNewLayout);
            mNewLayout = new LinearLayout(getActivity());
            mNewLayout.setOrientation(LinearLayout.HORIZONTAL);
            mButtonsSize = bounds.width();
            mNewLayout.addView(mBtn, params); // add button to a new layout so it won't be stretched because of it's width.
        }

    }

    mButtonsLayout.addView(mNewLayout); // add the last layout/ button.
}

你能详细解释一下吗?我必须达到相同的条件,但我不知道该怎么做,谢谢。 - Nitesh

1
您可以尝试以下操作。在添加按钮时,可以计算已占用的宽度。
occupiedWidth = button1Width + button2Width + ...

每个按钮的宽度=文本宽度+2*边距+2*填充
您可以通过以下方式获取放置在按钮上的文本的宽度
String buttonText = "<Your Button Text>"
paint.getTextBounds(buttonText, 0, buttonText.length(), bounds);
int textWidth = bounds.width();

将这个新按钮的边距和填充添加为:
int newWidth = textWidth + buttonMargin*2 + buttonPadding*2;

如果总宽度超过屏幕宽度,则移到下一行。

感谢您指导我如何获取文本大小。这帮助我找到了正确的方向来完成它,因此我将接受您的答案作为正确答案! - hardartcore

0
u can try this `enter code here`

 LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftMarginParams.leftMargin = 50;

        Button btn1 = new Button(this);
        btn1.setText("Button1");
        linLayout.addView(btn1, leftMarginParams)

0
根据我的看法,你有两个选择:
1)使用水平方向的线性布局(linear layout),并通过编程方式不断添加按钮到这个布局。如果按钮的宽度无法适应一行,它会自动移到下一行。
ViewGroup linearLayout = (ViewGroup) findViewById(R.id.linearLayoutID);

Button bt = new Button(this);
bt.setText("A Button");
bt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linerLayout.addView(bt);

2) 您可以按照 @kameswari 提到的方式计算每个按钮的 x、y 位置,并在相应的位置绘制相应的按钮。

Button button = (Button)findViewById(R.id.button);// or new Button(this);
AbsoluteLayout.LayoutParams absParams = 
    (AbsoluteLayout.LayoutParams)button.getLayoutParams();
absParams.x = X;
absParams.y = Y;
button.setLayoutParams(absParams);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接