ActionBar与TextView,ConstraintLayout问题重叠

时间:2019-04-24 22:43:59

标签: android android-constraintlayout

ActionBar与TextView重叠。为什么?约束是正确的恕我直言。它与RelativeLayout一起正常工作。我主要使用两个ConstraintLayout来添加一些不应影响ActionBar的填充。

代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ffffff"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="20dp"
        app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
        android:layout_marginTop="10dp"
        android:paddingRight="20dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:id="@+id/xxxx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xxxxxxx"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="#000000"
            app:layout_constraintTop_toTopOf="@+id/constraintLayout2"
            app:layout_constraintStart_toStartOf="@+id/constraintLayout2"/>

1 个答案:

答案 0 :(得分:1)

  

将第二个ConstraintLayout的高度更改为wrap_content,或者如果要全高,则将底部约束和高度设置为0dp,如下所示:

enter image description here

 <androidx.constraintlayout.widget.ConstraintLayout 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">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ffffff"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
        app:layout_constraintBottom_toBottomOf="parent">

        <TextView
            android:id="@+id/xxxx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xxxxxxx"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="#000000"
            app:layout_constraintStart_toStartOf="@+id/constraintLayout2"
            app:layout_constraintTop_toTopOf="@+id/constraintLayout2" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>