【问题标题】:Android: How to display a widget over another one using ConstraintLayout?Android:如何使用 ConstraintLayout 在另一个小部件上显示一个小部件?
【发布时间】:2019-02-19 15:40:45
【问题描述】:

我使用ConstraintLayout 并拥有三个小部件:一个View(这是一条与父级宽度匹配的简单线,即1dp 高度)和两个FloatingActionButton。这两个必须显示在View 上并与后者垂直居中。我将使用边距来分隔它们。我的问题是:如何使用 ConstraintLayout 在另一个小部件上显示一个小部件?

【问题讨论】:

    标签: android android-layout android-widget android-view android-constraintlayout


    【解决方案1】:

    我希望我能理解你的问题。您可以使用FrameLayout 或ConstraintLayout 解决方案。

    这是我使用 FrameLayout 的解决方案:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_gravity="center"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:background="@color/colorPrimary" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|start"
            android:layout_margin="16dp"
            android:src="@drawable/ic_launcher_foreground" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|end"
            android:layout_margin="16dp"
            android:src="@drawable/ic_launcher_foreground" />
    
    </FrameLayout>
    

    结果:

    这是我使用 ConstraintLayout 的解决方案:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 
        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:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteY="81dp">
    
        <View
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:layout_gravity="center"
            android:layout_margin="8dp"
            android:background="@color/colorPrimary"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|start"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="84dp"
            android:layout_marginStart="84dp"
            android:layout_marginTop="8dp"
            android:src="@drawable/ic_launcher_foreground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.5" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|end"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="84dp"
            android:layout_marginRight="84dp"
            android:layout_marginTop="8dp"
            android:src="@drawable/ic_launcher_foreground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.5" />
    
    </android.support.constraint.ConstraintLayout>
    

    结果:

    您可以使用不同的marings/padding 来满足您的需求。

    不要忘记将implementation 'com.android.support:design:27.1.1' 添加到您的build.gradle(Module:app) 文件中。

    【讨论】:

    • 其实FloatingActionButton和View之间没有联系吗?只在它们和 ConstraintLayout 之间?
    • 是的,View 和 FAB 之间没有约束。
    【解决方案2】:

    由于 grrigore 已经完美地回答了问题,我想指出:

    布局xml文件中widgets标签的顺序很重要,后一个会与前一个重叠,为避免这种情况,你可以在前一个widget中添加android:background属性来表示这是一个背景。强>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 2020-11-07
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 2019-11-19
      相关资源
      最近更新 更多