简体   繁体   中英

LinearLayout: 3 Elements, Top, Center, Bottom

I've the following xml file:

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

    <FrameLayout
        android:id="@+id/topFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <ImageView
            android:id="@+id/imageViewTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_00" />

        <TextView
            android:id="@+id/textViewTest"
            android:layout_below="@+id/imageViewTest"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="This is a Test"/>

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/bottomFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal" />

</LinearLayout>
  • The first FrameLayout contains a fragment, which contains some buttons.
  • The RelativeLayout contains for example an image (changig size) and a text.
  • The last FrameLayout contains also a fragment, which contains either some buttons or some text or an image.

My aim is, that: - the first FrameLayout is on top (that works) - the elements in the RelativeLayout are centered in the middle of the screen (doesn't work) - the last FrameLayout is on the bottom (doesn't work)

It should look like this:

在此处输入图片说明

My previous attempts with android:gravity="center" in the RelativeLayout and android:layout_gravity="bottom|center_horizontal" for the second FrameLayout failed.

Use relative layout and use android:layout_centerHorizontal="true" instead of gravity.

EDIT

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

<FrameLayout
    android:id="@+id/topFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/topFragment">

    <ImageView
        android:id="@+id/imageViewTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_00"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/textViewTest"
        android:layout_below="@+id/imageViewTest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is a Test"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

<FrameLayout
    android:id="@+id/bottomFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

</RelativeLayout>

Change the LineareLayout to Relative and use layout_centerInParent for the middle layout and alignParentBottom for the last one.

The top View will be anchored to the top by default

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> //removed orientation as it won't be needed anymore

    <FrameLayout
        android:id="@+id/topFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="wrap_content" // change here
        android:layout_height="wrap_content"
        android:layout_centernInParent="true">  // here

        <ImageView
            android:id="@+id/imageViewTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_00" />

        <TextView
            android:id="@+id/textViewTest"
            android:layout_below="@+id/imageViewTest"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="This is a Test"/>

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/bottomFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" /> //here

</RelativeLayout>

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