android-当每个按钮的宽度不同时动态添加按钮,当宽度满时自动流到下一行

问题描述 投票:-1回答:2

我需要动态地将按钮创建为线性布局。每个按钮的宽度将为wrap_content。整行填满后,应在下一行中创建按钮。

我已经在stackOverflow中看到了其他答案,但是在这种情况下,行中的按钮数量是恒定的。在我的情况下,它是动态的,因为每个按钮的宽度都是基于文本宽度的动态。

类似于在此站点中如何将标签添加到问题。

例如问题之一的答案是:

LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
layout.setOrientation(LinearLayout.VERTICAL);  //Can also be done in xml by android:orientation="vertical"


for (int i = 0; i < 3; i++) {
    LinearLayout row = new LinearLayout(this);
    row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    for (int j = 0; j < 4; j++) {
        Button btnTag = new Button(this);
        btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        btnTag.setText("Button " + (j + 1 + (i * 4));
        btnTag.setId(j + 1 + (i * 4));
        row.addView(btnTag);
    }
    layout.addView(row);
}

但是这里连续的按钮数目是恒定的。在我的情况下,这将有所不同,因为每个按钮的宽度都不同。

android button dynamic row
2个回答
1
投票

您需要一个FlowLayout,它是一个Extended linear layout that wrap its content when there is no place in the current line

签出:https://github.com/ApmeM/android-flowlayout


0
投票

您是否尝试过使用GridLayout?

    linearLayoutParent = findViewById(R.id.ll_buttons_parent); // container layout or parent layout - orientation vertical
    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.button_horizontal_layout, linearLayoutParent, false);
    LinearLayout linearLayoutButton = view.findViewById(R.id.ll_button_horizontal);
    int rows = 3;
    for (int r = 0; r < rows; r++) {
        GridLayout gridLayout = new GridLayout(this);
        gridLayout.setColumnCount(2);
        for (int i = 0; i < 4; i++) {
            Button optionButton = new Button(this);
            optionButton.setGravity(Gravity.CENTER);
            optionButton.setText(String.valueOf(i + 1));
            gridLayout.addView(optionButton); // adding buttons to grid layout
        }
        linearLayoutButton.addView(gridLayout); // adding grid layout to vertical linear layout row by row
    }
    linearLayoutParent.addView(linearLayoutButton); //adding linear layouts to container layout

容器:ll_buttons_parent

<LinearLayout
    android:id="@+id/ll_buttons_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintTop_toTopOf="parent"/>

button_horizo​​ntal_layout.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ll_button_horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#F8CCCC"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    app:layout_constraintTop_toTopOf="parent" />
© www.soinside.com 2019 - 2024. All rights reserved.