繁体   English   中英

LinearLayout:3个元素,顶部,中心,底部

[英]LinearLayout: 3 Elements, Top, Center, Bottom

我有以下xml文件:

<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>
  • 第一个FrameLayout包含一个片段,其中包含一些按钮。
  • RelativeLayout包含例如图像(更改大小)和文本。
  • 最后一个FrameLayout还包含一个片段,其中包含一些按钮或一些文本或图像。

我的目的是: -第一个FrameLayout在顶部(起作用)-RelativeLayout中的元素在屏幕中间居中(不起作用)-最后一个FrameLayout在底部(不起作用) )

它看起来应该像这样:

在此处输入图片说明

我先前在RelativeLayout使用android:gravity="center"和第二个FrameLayout进行android:layout_gravity="bottom|center_horizontal"android:layout_gravity="bottom|center_horizontal"失败。

使用相对布局,并使用android:layout_centerHorizontal="true"代替引力。

编辑

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

LineareLayout更改为Relative然后将layout_centerInParent用于中间布局,将alignParentBottom用于最后一个布局。

默认情况下,顶View将固定在顶部

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM