简体   繁体   English

动态添加按钮以滚动视图

[英]Dynamically adding buttons to scroll view

I would like some help with dynamically adding buttons to a ScrollView . 我想要一些将按钮动态添加到ScrollView I've got it working with a LinearLayout but obviously I can only add so many buttons before they no longer appear on screen. 我已经将它与LinearLayout配合使用,但是显然我只能添加这么多的按钮,然后它们才不再出现在屏幕上。 My code is below with an attached image of its current state. 我的代码在下面,并附有其当前状态的图像。

I tried changing every occurrence of LinearLayout with ScrollView in the code but when I ran it, I got an error that stated something along the lines of ScrollViews can only have 1 direct child . 我尝试用代码中的ScrollView更改每次LinearLayout出现,但是在运行它时,出现一个错误,该错误指出ScrollViews can only have 1 direct child

I'm not sure how to make it work, so if someone could give me some guidance on how to do it, I would be very grateful. 我不确定如何使它起作用,所以如果有人可以给我一些指导,我将不胜感激。

My XML Code: 我的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>

My Java Code (where I'm dynamically creating the buttons): 我的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);
        }
    }
}

Image of the current layout. 当前布局的图像。

图片

Yes ScrollView can only have 1 child, and often it is the ViewGroup like LinearLayout , RelativeLayout , etc. You need to wrap your LinearLayout with a ScrollView , like this: 是的, ScrollView只能有1个孩子,并且通常是ViewGroupLinearLayoutRelativeLayout等。您需要使用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>

Or you can change your topmost RelativeLayout into ScrollView if it has only one child. 或者,如果只有一个孩子,则可以将最上面的RelativeLayout更改为ScrollView

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

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