工具栏中的ConstraintLayout不将match_parent应用于宽度或高度

时间:2019-06-21 15:16:51

标签: android android-constraintlayout

我的应用程序顶部有一个Toolbar,其中包含一个ConstraintLayout,而其中依次包含一个ImageView,一个TextView和另一个TextView(标题)。我希望标题位于Toolbar的中央,因此我将标题的约束设置为在两侧相等(8和8)。但是在运行应用程序并检查了Android Studio中的布局预览后,我意识到尽管ConstraintLayout具有android:layout_width="match_parent"android:layout_height="match_parent"属性,但它并没有占用适当的空间,因此导致居中要关闭的标题(参见图片)。关于我所缺少的任何想法吗?

<?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_height="match_parent"
    android:layout_width="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/home_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/home_toolbar_gradient"
        android:elevation="4dp"
        android:minHeight="70dp"
        android:theme="@style/AppTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout_editor_absoluteX="16dp"
            tools:layout_editor_absoluteY="19dp">

            <ImageView
                android:id="@+id/weatherImg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:contentDescription="weather"
                android:src="@mipmap/weather_placeholder_icon"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="HardcodedText" />

            <TextView
                android:id="@+id/weatherTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start"
                android:layout_marginStart="8dp"
                android:fontFamily="@font/montserrat_bold"
                android:gravity="start"
                android:text="@string/home_weather_text_placeholder"
                android:textAlignment="center"
                android:textColor="@android:color/white"
                android:textSize="15sp"
                app:layout_constraintBottom_toBottomOf="@+id/weatherImg"
                app:layout_constraintStart_toEndOf="@+id/weatherImg"
                app:layout_constraintTop_toTopOf="@+id/weatherImg" />

            <TextView
                android:id="@+id/title_home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:fontFamily="@font/montserrat_bold"
                android:gravity="center"
                android:text="@string/home"
                android:textAlignment="center"
                android:textColor="@android:color/white"
                android:textSize="18sp"
                app:layout_constraintBottom_toBottomOf="@+id/weatherImg"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/weatherImg" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.appcompat.widget.Toolbar>

    <!-- Using a view with a gradient to create a drop navbar_shadow effect for the navbar -->
    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_above="@id/bottom_nav_bar"
        android:background="@drawable/navbar_shadow"
        app:layout_constraintBottom_toTopOf="@+id/bottom_nav_bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@android:color/white"
        app:itemIconTint="@drawable/nav_selected_item_color"
        app:itemTextColor="@drawable/nav_selected_item_color"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_bar">


    </com.google.android.material.bottomnavigation.BottomNavigationView>

</androidx.constraintlayout.widget.ConstraintLayout> 

P.S:我不知道这是否重要,但工具栏上还有一个menu,其中包含属性为showAsAction="always"的项目

layout_preview

1 个答案:

答案 0 :(得分:1)

enter image description here

  

尝试一下:您的疑惑是正确的。   对于工具栏

     

要删除左侧空间,请添加-> app:contentInsetStart

     

要删除右空间,请添加-> app:contentInsetEnd

<?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_height="match_parent"
            android:layout_width="match_parent">

    <androidx.appcompat.widget.Toolbar
            android:id="@+id/home_toolbar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:elevation="4dp"
            android:minHeight="70dp"
            android:theme="@style/AppTheme"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:contentInsetStart="0dp"
            app:contentInsetEnd="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            <ImageView
                    android:id="@+id/weatherImg"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="8dp"
                    android:contentDescription="weather"
                    android:src="@drawable/ic_cloud_black"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:ignore="HardcodedText"/>

            <TextView
                    android:id="@+id/weatherTxt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="start"
                    android:layout_marginStart="8dp"
                    android:gravity="start"
                    android:text="00"
                    android:textAlignment="center"
                    android:textColor="@android:color/white"
                    android:textSize="15sp"
                    app:layout_constraintBottom_toBottomOf="@+id/weatherImg"
                    app:layout_constraintStart_toEndOf="@+id/weatherImg"
                    app:layout_constraintTop_toTopOf="@+id/weatherImg"/>

            <TextView
                    android:id="@+id/title_home"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginStart="8dp"
                    android:layout_marginEnd="8dp"
                    android:gravity="center"
                    android:text="home"
                    android:textAlignment="center"
                    android:textColor="@android:color/white"
                    android:textSize="18sp"
                    app:layout_constraintBottom_toBottomOf="@+id/weatherImg"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/weatherImg"/>

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.appcompat.widget.Toolbar>
</androidx.constraintlayout.widget.ConstraintLayout>