相对布局重叠线性布局

时间:2016-10-09 09:50:55

标签: android-layout

我有以下代码片段:

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

   <RelativeLayout
    android:id="@+id/activity_post"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="nishs.com.simpleblog.PostActivity">

        <ImageButton
        android:id="@+id/imageSelect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:contentDescription="@string/add_image_description"
        android:cropToPadding="false"
        android:scaleType="centerCrop"
        android:src="@drawable/click_here_to_add_image" />

       <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageSelect"
        android:orientation="vertical"
        android:padding="@dimen/activity_horizontal_margin">

           <EditText
            android:id="@+id/titleField"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/input_outline"
            android:hint="@string/post_title"
            android:inputType="textCapWords"
            android:maxLines="1"
            android:padding="@dimen/input_padding"
            android:textColorHint="@color/grey" />

           <EditText
            android:id="@+id/descField"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:background="@drawable/input_outline"
            android:hint="@string/post_description"
            android:inputType="textMultiLine|textCapSentences"
            android:padding="@dimen/input_padding"
            android:textColorHint="@color/grey" />

        </LinearLayout>

       <Button
        android:id="@+id/submitBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        android:text="@string/post_button"
        android:textColor="@color/white"
        android:textStyle="bold" />

    </RelativeLayout>
</ScrollView>

描述EditText与提交按钮重叠,如链接的图像所示。有什么方法可以让LinearLayout出现在RelativLayout的Submit Post按钮上面吗?

Link to the image

1 个答案:

答案 0 :(得分:0)

您可以尝试两件事,看看哪种更适合您的需求。

您可以在EditTexts

下方对齐按钮
<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"
android:fillViewport="true">

<RelativeLayout
    android:id="@+id/activity_post"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="nishs.com.simpleblog.PostActivity">

    <ImageButton
        android:id="@+id/imageSelect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:contentDescription="@string/add_image_description"
        android:cropToPadding="false"
        android:scaleType="centerCrop"
        android:src="@drawable/click_here_to_add_image" />

    <LinearLayout
        android:id="@+id/edit_text_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageSelect"
        android:orientation="vertical"
        android:padding="@dimen/activity_horizontal_margin">

        <EditText
            android:id="@+id/titleField"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/input_outline"
            android:hint="@string/post_title"
            android:inputType="textCapWords"
            android:maxLines="1"
            android:padding="@dimen/input_padding"
            android:textColorHint="@color/grey" />

        <EditText
            android:id="@+id/descField"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:background="@drawable/input_outline"
            android:hint="@string/post_description"
            android:inputType="textMultiLine|textCapSentences"
            android:padding="@dimen/input_padding"
            android:textColorHint="@color/grey" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/button_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/edit_text_container"
        android:adjustViewBounds="true"
        android:gravity="bottom">

        <Button
            android:id="@+id/submitBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="@string/post_button"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

    </LinearLayout>

</RelativeLayout>

或者您可以将按钮放在ScrollView外部:

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

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

    <RelativeLayout
        android:id="@+id/activity_post"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context="nishs.com.simpleblog.PostActivity">

        <ImageButton
            android:id="@+id/imageSelect"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:adjustViewBounds="true"
            android:contentDescription="@string/add_image_description"
            android:cropToPadding="false"
            android:scaleType="centerCrop"
            android:src="@drawable/click_here_to_add_image" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/imageSelect"
            android:orientation="vertical"
            android:padding="@dimen/activity_horizontal_margin">

            <EditText
                android:id="@+id/titleField"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/input_outline"
                android:hint="@string/post_title"
                android:inputType="textCapWords"
                android:maxLines="1"
                android:padding="@dimen/input_padding"
                android:textColorHint="@color/grey" />

            <EditText
                android:id="@+id/descField"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:background="@drawable/input_outline"
                android:hint="@string/post_description"
                android:inputType="textMultiLine|textCapSentences"
                android:padding="@dimen/input_padding"
                android:textColorHint="@color/grey" />

        </LinearLayout>

    </RelativeLayout>
</ScrollView>

<Button
    android:id="@+id/submitBtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/colorPrimary"
    android:text="@string/post_button"
    android:textColor="@color/white"
    android:textStyle="bold" />