将我的RelativeLayout放在ScrollView中会错放布局中的所有项目

Lis*_*nne 2 android scrollview relativelayout android-layout

非常感谢您的帮助和关注.

在将RelativeLayout放入ScrollView之前,这就是我的Activity的样子:

在此输入图像描述

我仍然需要努力,但按钮,EditTexts和视图都在他们的位置.


现在,如果我将RelativeLayout放在ScrollView中,那么项目的所有位置都会混乱,

这是(不希望的)结果

在此输入图像描述

请问我该如何解决这个问题?

这是我的XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bgland"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MenuActivity" >
<View
    android:id="@+id/view1"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_centerInParent="true"
    android:layout_margin="20dip"
    android:background="@android:color/darker_gray" />
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:scaleType="fitXY"
    android:src="@drawable/white_bg" />

<EditText
    android:id="@+id/bikenumber"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/getbikebutton"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:layout_marginTop="20dip"
    android:ems="10"
    android:hint="@string/hint_getbike"
    android:inputType="number"
    android:singleLine="true" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/getbikebutton"
    style="@style/OrangeButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/view1"
    android:layout_alignLeft="@+id/view1"
    android:layout_alignRight="@+id/view1"
    android:layout_marginBottom="10dip"
    android:layout_marginLeft="15dip"
    android:layout_marginRight="15dip"
    android:text="@string/menu_getbikebuttontext" />



<Button
    android:id="@+id/buttonGoToMyLoc"
    style="@style/OrangeButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/view1"
    android:layout_alignRight="@+id/view1"
    android:layout_below="@+id/view1"
    android:layout_marginBottom="10dip"
    android:layout_marginLeft="15dip"
    android:layout_marginRight="15dip"
    android:text="@string/menu_text_near_me" />

<Button
    android:id="@+id/buttonGoToThisLoc"
    style="@style/OrangeButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/view1"
    android:layout_alignRight="@+id/view1"
    android:layout_below="@+id/buttonGoToMyLoc"
    android:layout_marginLeft="15dip"
    android:layout_marginRight="15dip"
    android:layout_marginTop="10dip"
    android:text="@string/menu_text_address" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/imageView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="25dip"
    android:text="@string/menu_title"
    android:textColor="@android:color/black"
    android:textSize="20dip"
    android:textStyle="bold" />

<EditText
    android:id="@+id/locAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_alignRight="@+id/imageView1"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:layout_marginTop="10dip"
    android:ems="10"
    android:hint="@string/menu_hint"
    android:inputType="text"
    android:singleLine="true"
    android:visibility="gone" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/locAddressCity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_alignRight="@+id/imageView1"
    android:layout_below="@+id/locAddress"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:ems="10"
    android:hint="@string/menu_hint_city"
    android:inputType="text"
    android:singleLine="true"
    android:visibility="gone" />

</RelativeLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

小智 8

添加android:fillViewport="true"到您的ScrollView代码,如下所示:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="5dp"
            tools:context=".MenuActivity" >

            <View
                android:id="@+id/view1"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_centerInParent="true"
                android:layout_margin="20dip"
                android:background="@android:color/darker_gray" />

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:scaleType="fitXY"
                android:src="@android:color/white" />

            <EditText
                android:id="@+id/bikenumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/getbikebutton"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="20dip"
                android:layout_marginRight="20dip"
                android:layout_marginTop="20dip"
                android:ems="10"
                android:hint="Get Bikes"
                android:inputType="number"
                android:singleLine="true" >

                <requestFocus />
            </EditText>

            <Button
                android:id="@+id/getbikebutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/view1"
                android:layout_alignLeft="@+id/view1"
                android:layout_alignRight="@+id/view1"
                android:layout_marginBottom="10dip"
                android:layout_marginLeft="15dip"
                android:layout_marginRight="15dip"
                android:text="FindBikes" />

            <Button
                android:id="@+id/buttonGoToMyLoc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/view1"
                android:layout_alignRight="@+id/view1"
                android:layout_below="@+id/view1"
                android:layout_marginBottom="10dip"
                android:layout_marginLeft="15dip"
                android:layout_marginRight="15dip"
                android:text="NearMe" />

            <Button
                android:id="@+id/buttonGoToThisLoc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/view1"
                android:layout_alignRight="@+id/view1"
                android:layout_below="@+id/buttonGoToMyLoc"
                android:layout_marginLeft="15dip"
                android:layout_marginRight="15dip"
                android:layout_marginTop="10dip"
                android:text="Near Address" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/imageView1"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="25dip"
                android:text="Title"
                android:textColor="@android:color/black"
                android:textSize="20dip"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/locAddress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/imageView1"
                android:layout_alignRight="@+id/imageView1"
                android:layout_below="@+id/textView1"
                android:layout_centerHorizontal="true"
                android:textColor="@android:color/black"
                android:layout_marginLeft="20dip"
                android:layout_marginRight="20dip"
                android:layout_marginTop="10dip"
                android:ems="10"
                android:hint="Addresss"
                android:inputType="text"
                android:singleLine="true"
                android:visibility="gone" >

                <requestFocus />
            </EditText>

            <EditText
                android:id="@+id/locAddressCity"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/imageView1"
                android:layout_alignRight="@+id/imageView1"
                android:layout_below="@+id/locAddress"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="20dip"
                android:layout_marginRight="20dip"
                android:ems="10"
                android:hint="Address City"
                android:inputType="text"
                android:singleLine="true"
                android:visibility="gone" />
        </RelativeLayout>
    </LinearLayout>

</ScrollView>
Run Code Online (Sandbox Code Playgroud)

ScrollView随附,LinearLayout它只适用于直接的孩子ScrollView.您可以将所有组件放在LinearLayout.So中,如果您想要内部任何布局,ScrollView那么您应该首先放入LinearLayout内部ScrollView并将所有组件放入其中LinearLayout.