android - 相对布局 : centering of two FrameLayout elements

标签 android centering android-relativelayout

在我的 activity_main.xml 中,我有许多元素,其中我想让两个 FrameLayout 元素居中。如果我只有两个 FrameLayout 元素,它们就会正确居中。以下是显示的代码和屏幕截图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="com.abcd.testlayout.MainActivity">

    <FrameLayout
        android:id="@+id/vertical_line"
        android:background="@color/colorPrimary"
        android:layout_width="50dp"
        android:layout_height="280dp">
        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:srcCompat="@android:drawable/presence_audio_away" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/vertical_line2"
        android:background="@color/colorAccent"
        android:layout_width="280dp"
        android:layout_height="50dp"
        android:layout_below="@+id/vertical_line">
        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:srcCompat="@android:drawable/ic_lock_idle_alarm" />
    </FrameLayout>

</RelativeLayout>

如您所见,我有两条线,一条垂直的蓝线和一条水平的粉红色线。粉色线水平居中,蓝色线垂直居中。这正是我想要的。

enter image description here

现在,如果我将所有其他元素添加到布局中,这里是代码和屏幕截图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="com.abcd.testlayout.MainActivity">

    <FrameLayout
        android:id="@+id/vertical_line"
        android:background="@color/colorPrimary"
        android:layout_width="50dp"
        android:layout_height="280dp">
        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:srcCompat="@android:drawable/presence_audio_away" />
    </FrameLayout>

    <FrameLayout
        android:id="@+id/vertical_line2"
        android:background="@color/colorAccent"
        android:layout_width="280dp"
        android:layout_height="50dp"
        android:layout_below="@+id/vertical_line">
        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:srcCompat="@android:drawable/ic_lock_idle_alarm" />
    </FrameLayout>

    <ImageView
        android:id="@+id/img1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter" />

    <RelativeLayout
        android:gravity="center"
        android:id="@+id/rel"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:antialias="true"
            android:scaleType="matrix" />
    </RelativeLayout>

    <ImageButton
        android:id="@+id/img3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        app:srcCompat="@android:drawable/btn_default_small"/>

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/imageButton"
        android:layout_marginStart="42dp"
        android:layout_marginLeft="42dp"
        app:srcCompat="@android:drawable/arrow_up_float"/>

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginEnd="30dp"
        android:layout_marginRight="30dp"
        android:layout_above="@+id/seekBar"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        app:srcCompat="@android:drawable/arrow_down_float"/>

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:max="100"
        android:progress="0"
        android:visibility="visible"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true" />

</RelativeLayout>

如您所见,蓝线和红线移到了左上角,但我喜欢这两个元素的位置与上一个屏幕截图中的位置保持一致。

有没有办法做到这一点,意思是将蓝线和红线移动到与上一个屏幕截图完全相同的位置,并保持其他元素的位置不变,而不尝试向这些元素添加左边距或按钮边距?适用于所有手机尺寸甚至平板电脑的东西,只需使用 gravity:center 或类似的东西?

enter image description here

最佳答案

将这两个 View 包装在另一个 RelativeLayout 中,如下所示:

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        tools:context=".MainActivity">

        <FrameLayout
            android:id="@+id/vertical_line"
            android:layout_width="50dp"
            android:layout_height="280dp"
            android:background="@color/colorPrimary">

            <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                app:srcCompat="@android:drawable/presence_audio_away" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/vertical_line2"
            android:layout_width="280dp"
            android:layout_height="50dp"
            android:layout_below="@+id/vertical_line"
            android:background="@color/colorAccent">

            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                app:srcCompat="@android:drawable/ic_lock_idle_alarm" />
        </FrameLayout>
    </RelativeLayout>

    <!-- Your other views -->

</RelativeLayout>

enter image description here

关于android - 相对布局 : centering of two FrameLayout elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46746135/

相关文章:

javascript - 错误无法读取未定义的属性(读取 'configurations')。类型错误 : Cannot read properties of undefined (reading 'configurations' )

java - 为什么在将数据从 Activity 传递到 Fragment 时会出现空指针异常?

java - 无法在 Snackbar 中设置计时器

html - 在html css中使网格居中的问题

android RelativeLayout 错误 "No resource identifier found...."

java - 即使不需要 ScrollView,如何使 Activity 可滚动?

c# - 如何使标签在 WinForms 中居中?

python - Plotly Dash 中心 dcc.输入文本字段

Android RelativeLayout 和 wrap_content 的高度?

android - 如何以编程方式添加水平分隔线