gpt4 book ai didi

android - 以编程方式用按钮填充屏幕

转载 作者:行者123 更新时间:2023-11-29 15:58:04 25 4
gpt4 key购买 nike

我正在尝试以编程方式将按钮添加到布局中,以便它们自动将自己定位在现有按钮的旁边,或者如果它们不合适则放在新的一行上。

我不确定我的解释是否正确,所以举一个我想要达到的结果的例子:

手机屏幕:

|[button1][button2][button3]|
|[sort][LongButton][sort] |
|[tooLongSoGoesNextRow]... |

到目前为止,我已经设法自动将它们放在同一行中,但如果它们不再适合当前行,则无法让它们“跳转”到下一行。我来自 HTML,我认为这很容易做到,但要么我做错了,要么...

这是我的布局目前的样子:

<LinearLayout
android:layout_width="fill_parent"
android:id="@+id/buttonsLayout"
android:layout_height="wrap_content">
</LinearLayout>

我用来添加按钮的代码:

   LinearLayout layout = (LinearLayout) findViewById(R.id.buttonsLayout);

//set the properties for button
Button btnTag = new Button(getApplicationContext());
btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
btnTag.setText("WHATEVER");
btnTag.setId(generator.nextInt());

//add button to the layout
layout.addView(btnTag);

这会在同一行添加按钮,不会相互重叠,但它们会一直紧挨着添加,即使它们不适合屏幕。

最佳答案

最终,我用自定义 ViewGroup 解决了这个问题(遵循这个 http://hzqtc.github.io/2013/12/android-custom-layout-flowlayout.html ):

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class FlowLayout extends ViewGroup {

private int paddingHorizontal;
private int paddingVertical;

public FlowLayout(Context context) {
super(context);
init();
}

public FlowLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public FlowLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init() {
paddingHorizontal = 0;
paddingVertical = 0;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int childLeft = getPaddingLeft();
int childTop = getPaddingTop();
int lineHeight = 0;
// 100 is a dummy number, widthMeasureSpec should always be EXACTLY for FlowLayout
int myWidth = resolveSize(100, widthMeasureSpec);
int wantedHeight = 0;
for (int i = 0; i < getChildCount(); i++) {
final View child = getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
}
// let the child measure itself
child.measure(
getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutParams().width),
getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutParams().height));
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
// lineheight is the height of current line, should be the height of the heightest view
lineHeight = Math.max(childHeight, lineHeight);
if (childWidth + childLeft + getPaddingRight() > myWidth) {
// wrap this line
childLeft = getPaddingLeft();
childTop += paddingVertical + lineHeight;
lineHeight = childHeight;
}
childLeft += childWidth + paddingHorizontal;
}
wantedHeight += childTop + lineHeight + getPaddingBottom();
setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec));
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int childLeft = getPaddingLeft();
int childTop = getPaddingTop();
int lineHeight = 0;
int myWidth = right - left;
for (int i = 0; i < getChildCount(); i++) {
final View child = getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
}
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
lineHeight = Math.max(childHeight, lineHeight);
if (childWidth + childLeft + getPaddingRight() > myWidth) {
childLeft = getPaddingLeft();
childTop += paddingVertical + lineHeight;
lineHeight = childHeight;
}
child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
childLeft += childWidth + paddingHorizontal;
}
}
}

在布局中:

    <my.package.app.FlowLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonsContainer"/>

并以编程方式添加按钮:

            ViewGroup flowContainer = (ViewGroup) findViewById(R.id.buttonsContainer);
Button btnTag = new Button(getApplicationContext());
btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
btnTag.setText("whatever");
btnTag.setId(23425);
flowContainer.addView(btnTag);

希望对大家有所帮助。现在我只是在努力添加一个垂直滚动条:)。

最好的问候,哈维尔

关于android - 以编程方式用按钮填充屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26755771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com