【问题标题】:LinearLayout: 3 Elements, Top, Center, Bottom线性布局:3 个元素,顶部、中心、底部
【发布时间】:2015-08-11 03:55:21
【问题描述】:

我有以下 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 在底部(不起作用)

应该是这样的:

我之前在RelativeLayoutandroid:layout_gravity="bottom|center_horizontal" 中使用android:gravity="center" 的第二次FrameLayout 尝试失败了。

【问题讨论】:

    标签: java android xml android-layout android-fragments


    【解决方案1】:

    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>
    

    【讨论】:

    • 谢谢。 FrameLayout 片段现在位于底部。但是图片和文字不是水平居中的(仅垂直居中)
    • @Struct 我已更新。您需要更改该内部 RL 的宽度。
    • 将内部 RelativeLayout 更改为 LinearLayout 以获得更好的性能
    • @Lamorak 是的,LinearLayout 在这里是有意义的,并且可以具有更好的性能,但在简单的布局中不会注意到差异。但是,对于更复杂的布局,这可能是一个因素。
    【解决方案2】:

    使用相对布局并使用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>
    

    【讨论】:

    • 也谢谢你。这是与其他答案相同的问题。 FrameLayout 片段现在位于底部。但是图片和文字不是水平居中的(仅垂直居中)
    • 向其他元素添加水平中心,应该没问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多