简体   繁体   中英

Increasing scrollview by adding buttons in a relativelayout (or linearlayout ) from bottom to top

Could really use some help with this one.

I'm trying to start with a single button at the bottom of my screen. When my counter++ button is clicked, a second button will be dynamically created above the button at the bottom of the screen. I was able to get this far, the problem is that the height of my scrollview doesn't increase when my buttons reach the top of the screen. Please see my example below.

Example

I'm assuming the scrollview only increases when items are added below the current view although if that's the case, is there no way I can create several buttons starting at the bottom going upwards?

Here my 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="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>

Java:

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);

}

I've also tried doing this in a linear layout to see if the scrollview would increase but I couldn't figure out how to stack buttons from bottom to top in a linear layout. :S

Thanks in advance, yall are awesome!

Here is your designing part happy coding.....

<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>

@Texas use below code using that you are achieving what you want:

<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">


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

        <LinearLayout
            android:id="@+id/lladdViews"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="bottom"
            android:orientation="vertical">

            <Button
                android:id="@+id/floor0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="0" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

and here is Java File:

public class SampleActivity extends Activity {
    LinearLayout lladdViews;
    Button count;
    int floor = 0;
    private int margincount = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_activity);
        lladdViews = (LinearLayout) findViewById(R.id.lladdViews);
        count = (Button) findViewById(R.id.count);
        count.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addfloor();
            }
        });
    }


    public void addfloor() {

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

        RelativeLayout ll = new RelativeLayout(this);
        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);
        lladdViews.addView(ll);
    }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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