【问题标题】:Dynamically adding buttons to scroll view动态添加按钮到滚动视图
【发布时间】:2018-06-24 20:23:49
【问题描述】:

我想要一些关于动态添加按钮到ScrollView 的帮助。我已经用LinearLayout 让它工作了,但显然我只能在它们不再出现在屏幕上之前添加这么多按钮。我的代码在下面附有其当前状态的图像。

我尝试在代码中用ScrollView 更改每次出现的LinearLayout,但是当我运行它时,我收到一个错误,说明类似于ScrollViews can only have 1 direct child

我不知道如何让它工作,所以如果有人能给我一些关于如何做的指导,我将非常感激。

我的 XML 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    <LinearLayout
        android:id="@+id/mainLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@id/imageTextView"
        android:layout_centerHorizontal="true">
    </LinearLayout>

</RelativeLayout>

我的 Java 代码(我在其中动态创建按钮):

public class Main5Activity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);

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

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

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

当前布局的图像。

【问题讨论】:

    标签: java android xml button


    【解决方案1】:

    是的ScrollView只能有1个孩子,通常是ViewGroup,如LinearLayoutRelativeLayout等。 你需要用ScrollView 包裹你的LinearLayout,像这样:

    <ScrollView>
        <LinearLayout
        android:id="@+id/mainLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@id/imageTextView"
        android:layout_centerHorizontal="true"/>
    </ScrollView>
    

    或者,如果它只有一个孩子,您可以将最上面的 RelativeLayout 更改为 ScrollView

    【讨论】:

    • 我将不得不尝试您的第一个建议,将LinearLayout 包装在ScrollView 中。我无法将最顶部的RelativeLayout 更改为ScrollView,因为我在其中还有其他东西,我正在使用RelativeLayout 进行组织。我没有包括它,因为我认为它不相关(事后看来,知道它们在那里可能很有用)。无论如何,对于您的第一个建议,我是否需要更改 Java 代码本身的任何内容(我想只需将代码中的每个 LinearLayout 更改为 ScrollView)或者我可以将 LinearLayout 包装在 ScrollView在 XML 中?
    猜你喜欢
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多