首页 > 解决方案 > 通过在相对布局(或线性布局)中从下到上添加按钮来增加滚动视图

问题描述

真的可以在这方面使用一些帮助。

我试图从屏幕底部的一个按钮开始。单击我的 counter++ 按钮时,将在屏幕底部的按钮上方动态创建第二个按钮。我能够做到这一点,问题是当我的按钮到达屏幕顶部时,我的滚动视图的高度没有增加。请看我下面的例子。

例子

我假设滚动视图仅在当前视图下方添加项目时才会增加,但如果是这种情况,我有没有办法从底部开始创建几个向上的按钮?

这是我的代码。

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="wrap_content"
    android:layout_height="wrap_content"
    tools:context="com.example.bbetzner.ttt.Map">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    >

    <RelativeLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        >

    <Button
        android:id="@+id/count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="count++"

        />

    <Button
        android:id="@+id/floor0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="0"

        />

    </RelativeLayout>
</ScrollView>
</RelativeLayout>

爪哇:

public void addfloor(){

    Button myButton = new Button(this);
    myButton.setText(""+floor);
    margincount += 100;

    RelativeLayout ll = findViewById(R.id.layout);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ABOVE, R.id.floor0);
    lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
    lp.bottomMargin = margincount;
    ll.addView(myButton, lp);

}

我也尝试在线性布局中执行此操作,以查看滚动视图是否会增加,但我无法弄清楚如何在线性布局中从底部到顶部堆叠按钮。:S

在此先感谢,你们都很棒!

标签: javaandroidxmllayout

解决方案


这是您的设计部分快乐编码.....

<LinearLayout 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"
android:orientation="vertical"
>

   <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        >

        <Button
            android:id="@+id/count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="count++"

            />
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            >
        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center"

            >


            <Button
                android:id="@+id/floor0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="0"

                />
            <Button
                android:id="@+id/floor1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="7"

                />
            <Button
                android:id="@+id/floor2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="6"

                />
            <Button
                android:id="@+id/floor3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="5"

                />
            <Button
                android:id="@+id/floor4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="4"

                />
            <Button
                android:id="@+id/floor5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="0"

                /><Button
            android:id="@+id/floor6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="3"

            /><Button
            android:id="@+id/floor7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="2"

            />
            <Button
                android:id="@+id/floor8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="1"

                />
            <Button
                android:id="@+id/floor9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="0"

                />


        </LinearLayout>
        </ScrollView>
    </LinearLayout>


推荐阅读