【问题标题】:How to design overlapping elements in Android?如何在 Android 中设计重叠元素?
【发布时间】:2018-05-04 15:32:38
【问题描述】:

我正在尝试实现一个与此类似的屏幕:

我能够实现上述。我使用了以下逻辑:

FrameLayout
  |- Linear Layout
    |- Button (Purple Colour, Layout Width = 1)
    |- Button (Blue Colour, Layout Width = 1)
  |- Linear Layout (Layout_Gravity = center)
    |- Button (Button Text)

源代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/accent_material_light"
        android:orientation="vertical"
        android:padding="0dp"
        android:weightSum="100">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="40"
            android:background="@android:color/holo_purple" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="60"
            android:background="@android:color/holo_blue_light" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:background="@color/abc_search_url_text_pressed">

        <Button
            android:id="@+id/button2"
            android:layout_height="48dp"
            android:text="Button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="match_parent" />
    </LinearLayout>


</FrameLayout>

问题:-

如果两个视图(紫色和蓝色视图)必须分别占据屏幕的 40% 和 60%,我应该如何将 Button 放置在两个视图的边框上。在这种情况下,我不能使用 layout_gravity 居中。如何解决这个问题?

看起来像这样:-

如何解决这个问题?

【问题讨论】:

  • 对我来说,我会将FrameLayout 替换为RelativeLayout,删除第二个LinearLayout 并在按钮上添加`android:layout_below="@+id/imageButton"` 并且可能会给一些上边距。

标签: android layout android-linearlayout android-framelayout


【解决方案1】:

为了获得更好的性能,有必要减少层次结构。在这里,您可以使用唯一的约束布局作为根布局,而不是使用两/三级层次结构。

在约束布局中采用具有约束的按钮

 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"

这会将按钮设置在中心,然后设置两个图像视图

图像视图1:

 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintBottom_toTopOf="id:button"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"

imageView2:

 app:layout_constraintTop_toBottomOf="id:button"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"

多级层级加载 UI 需要很长时间。所以,尽量减少它。

【讨论】:

  • 感谢@Meet Patel 回答问题。我会尝试并接受。还有一个问题 - 你如何确保按钮位于两个线性布局之上而不是它们之下?
  • 在约束布局中,我们定义了每个视图的约束。在这里,我分配了按钮的 4 个不同的约束,尝试将其呈现为左、右、上和下。最终按钮将占据中心位置。在 Imageview 中,我给出按钮的底部约束,它将图像中心放置在剩余空间中。
  • 能否请您显示一个屏幕截图,显示您已实现 OP 问题中要求的布局(他们的第一个屏幕截图)?根据您的解释,视图中间会有一个与按钮大小完全相同的间隙。
【解决方案2】:

更简单的解决方案可以是ConstraintsLayout。下面是线性布局和相对布局相结合的解决方案。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/colorPrimaryDark"
        android:gravity="center">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_purple"
            android:src="@mipmap/ic_launcher" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/colorPrimary"
        android:gravity="center">

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_light"
            android:src="@mipmap/ic_launcher" />
    </LinearLayout>

</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Install" />
</RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2020-01-24
    • 2018-05-03
    • 2021-12-20
    相关资源
    最近更新 更多