Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

android - How to make a static button under a ScrollView?

I have the following XML. I'm trying to make a static button underneath my ScrollView. I've tried to set weights, set the button to be below the scrollview, etc. Can someone give me a way that I can get the button to stay at the bottom and the scrollview only take up the middle of the screen?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">



    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/menuButtons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <Button xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/newItems"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Items" />
        <Button xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/categories"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Categories" />

    </LinearLayout>


    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/contentScroller"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/menuButtons">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">


            <TableLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="1" >


                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip" >
                    <ImageView
                        android:src="@drawable/thumbdrive"/>"
                    <TextView
                        android:layout_column="1"            
                        android:text="Thumb Drives"            
                        android:padding="3dip" 
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <ImageView
                        android:src="@drawable/laptop"/>
                    <TextView
                        android:layout_column="1"
                        android:text="Laptops"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <ImageView
                        android:src="@drawable/sdcard"/>
                    <TextView
                        android:layout_column="1"
                        android:text="SD Cards"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <TextView
                        android:layout_column="1"
                        android:text="Other"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <TextView
                        android:layout_column="1"
                        android:text="Other"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <TextView
                        android:layout_column="1"
                        android:text="Other"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <TextView
                        android:layout_column="1"
                        android:text="Other"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <TextView
                        android:layout_column="1"
                        android:text="Other"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <TextView
                        android:layout_column="1"
                        android:text="Other"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Submit New Item"
        android:layout_below="@id/contentScroller"/>

</RelativeLayout>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I know this is really late but there is a better solution. The problem with the RelativeLayout approach and aligning the buttons to the bottom of the relative layout is that it forces the layout height to essentially be the same as fill_parent (or match_parent).

The proper way to do this is as follows:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fillViewport="true">
            <!-- Your Scrollview content goes here -->
    </ScrollView>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:text="Button Text Goes Here" />
</LinearLayout>

The key is to set the height to 0 and then give it a layout weight of 1... From what I can tell from reading up on the layout sizing process, giving it a size of 0 and a layout weight causes it to hold off on sizing the view until after it has processed all the children in the layout... It then comes around on a second pass and is able to size the scrollview properly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...