简体   繁体   中英

android - side by side buttons with scroll view

I'm trying to create a layout that has two buttons of equal width across the top of the screen and then a scroll view directly beneath it. I have a linearLayout inside of the scroll view because that's where i'm creating UI elements dynamically.

The buttons are positioned correctly but my scroll view is hidden or the linearlayout inside of the scrollview is hidden, i can't tell and don't know enough about layouts to determine what's happening. What am i doing wrong? I've created a TextView placeholder in the LinearLayout to tell if i can see anything and it's not showing up.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/EditRecordOuterLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:orientation="horizontal"
    >

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <RelativeLayout
            android:id="@+id/Bforeground"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center" >

            <TextView
                android:id="@+id/cancelButtonText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:textSize="20dp"
                android:text="Cancel" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@id/cancelButtonText"
                android:paddingBottom="10dip"
                android:paddingTop="10dip"
                android:src="@drawable/cancel" />
        </RelativeLayout>
                    <Button
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_alignBottom="@id/Bforeground"
            android:layout_alignLeft="@id/Bforeground"
            android:layout_alignRight="@id/Bforeground"
            android:layout_alignTop="@+id/Bforeground"
            android:onClick="clickedMe"
            android:gravity="center" />
    </RelativeLayout>

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <RelativeLayout
            android:id="@+id/foreground"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center" >

            <TextView
                android:id="@+id/button_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:textSize="20dp"
                android:text="Save" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@id/button_text"
                android:paddingBottom="10dip"
                android:paddingTop="10dip"
                android:src="@drawable/save" />
        </RelativeLayout>
                    <Button
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_alignBottom="@id/foreground"
            android:layout_alignLeft="@id/foreground"
            android:layout_alignRight="@id/foreground"
            android:layout_alignTop="@+id/foreground"
            android:onClick="clickedMe"
            android:gravity="center" />
    </RelativeLayout>
</LinearLayout>
<ScrollView
    android:id="@+id/editRecordScrollView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/editRecordLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
                     <TextView
                android:id="@+id/someText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:textSize="20dp"
                android:text="This text should be showing..." />
    </LinearLayout>
</ScrollView>

Got it! thanks to you guys. Fixed version below.

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

     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:orientation="horizontal"
        >

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <RelativeLayout
                android:id="@+id/Bforeground"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView
                    android:id="@+id/cancelButtonText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:textSize="20dp"
                    android:text="Cancel" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@id/cancelButtonText"
                    android:paddingBottom="10dip"
                    android:paddingTop="10dip"
                    android:src="@drawable/cancel" />
            </RelativeLayout>
                        <Button
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_alignBottom="@id/Bforeground"
                android:layout_alignLeft="@id/Bforeground"
                android:layout_alignRight="@id/Bforeground"
                android:layout_alignTop="@+id/Bforeground"
                android:onClick="clickedMe"
                android:gravity="center" />
        </RelativeLayout>

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <RelativeLayout
                android:id="@+id/foreground"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <TextView
                    android:id="@+id/button_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:textSize="20dp"
                    android:text="Save" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@id/button_text"
                    android:paddingBottom="10dip"
                    android:paddingTop="10dip"
                    android:src="@drawable/save" />
            </RelativeLayout>
                        <Button
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_alignBottom="@id/foreground"
                android:layout_alignLeft="@id/foreground"
                android:layout_alignRight="@id/foreground"
                android:layout_alignTop="@+id/foreground"
                android:onClick="clickedMe"
                android:gravity="center" />
        </RelativeLayout>
    </LinearLayout>
    <ScrollView
        android:id="@+id/editRecordScrollView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

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

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

You must place all UI elements inside of a single parent Layout. I suggest that you can still use a LinearLayout as your top level, but it should be vertical, like this:

android:orientation="vertical"

Within the parent vertical LinearLayout you may place a horizontal LinearLayout for the top buttons AND a ScrollView . That should do it.

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