【问题标题】:XML constraint layout: Elements placed on top of each other, fix?XML 约束布局:元素相互叠加,修复了吗?
【发布时间】:2019-05-22 00:58:37
【问题描述】:

我的 XML 代码如下所示:

<?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/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".BottomNavActivity">

    <TextView
            android:id="@+id/message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:background="#CCB0F0"
            android:text="Map"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/idCardView" app:layout_constraintEnd_toEndOf="parent"/>

    <android.support.v7.widget.CardView
            android:id="@+id/idCardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_marginTop="5dp"
            app:cardCornerRadius="4dp" app:layout_constraintTop_toTopOf="parent"
            tools:layout_editor_absoluteX="5dp" android:layout_marginBottom="332dp"
            app:layout_constraintTop_toBottomOf="@+id/message"
            app:layout_constraintBottom_toTopOf="@+id/container">
        <fragment android:id="@+id/autocomplete_fragment"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
        />
    </android.support.v7.widget.CardView>

    <android.support.constraint.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/container"
            app:layout_constraintTop_toBottomOf="@+id/idCardView"
            app:layout_constraintBottom_toTopOf="@+id/navigation" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="1.0">

    </android.support.constraint.ConstraintLayout>

    <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="0dp"
            android:layout_marginStart="0dp"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/navigation_menu"/>



</android.support.constraint.ConstraintLayout>

我希望TextView 在顶部,然后是CardView,然后是ConstraintLayout,然后是bottomNavigationView。不知何故,这些元素像这样堆叠在一起:

我确保每个元素分别被限制在下一个元素的顶部或底部。他们最终还是在彼此之上。有没有更好的方法或解决此问题的方法?

【问题讨论】:

    标签: android xml layout


    【解决方案1】:

    在卡片视图中,您在顶部添加了两个约束:删除 app:layout_constraintTop_toTopOf="parent" 并且将起作用。

    【讨论】:

      【解决方案2】:
      • 您不需要将 TextView 限制为 Carview,只需将 CardView 限制为 TextView。
      • 如果 TextView 宽度为 match_parent,则无需设置右约束。只有顶部和左侧。
      • 将 CarView 移除到父级顶部约束
      • 您不需要将 CardView 限制为 Container,只需将容器限制为 CardView

        <TextView
            android:id="@+id/message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:background="#CCB0F0"
            android:text="Map"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
        />
        
        <android.support.v7.widget.CardView
            android:id="@+id/idCardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_marginTop="5dp"
            app:cardCornerRadius="4dp" 
            tools:layout_editor_absoluteX="5dp" 
            android:layout_marginBottom="332dp"
            app:layout_constraintTop_toBottomOf="@+id/message"
            app:layout_constraintLeft_toLeftOf="parent"
            >
        
            <fragment android:id="@+id/autocomplete_fragment"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
        />
        

      【讨论】:

        猜你喜欢
        • 2021-12-24
        • 1970-01-01
        • 1970-01-01
        • 2019-03-29
        • 2018-09-28
        • 2018-03-15
        • 2019-05-31
        • 2020-12-03
        • 1970-01-01
        相关资源
        最近更新 更多