【问题标题】:Toolbar doesn't allow to use a match-parent layout in it工具栏不允许在其中使用匹配父布局
【发布时间】:2021-01-21 13:26:27
【问题描述】:

我设计了一个工具栏,但它不允许我制作匹配父级布局。它的左侧被锁定,它不允许我将 TextView 放在那里,但右侧做得很好;

我在 YouTube 上看过太多关于设计工具栏的视频,但他们没有这个问题。

???? You can see that in this picture.

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar 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="?attr/actionBarSize"
    android:background="@color/colorPrimaryDark"
    android:elevation="10dp">

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

        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/icon_more"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="?attr/actionBarSize"
            android:src="@drawable/ic_more_vert"
            android:tint="@android:color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/icon_search"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="?attr/actionBarSize"
            android:src="@drawable/ic_search"
            android:tint="@android:color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/icon_more"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="چی بخوریم ؟"
            android:textColor="@android:color/white"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.appcompat.widget.Toolbar>

build.gradle(模块:app):

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    implementation 'com.google.android.material:material:1.2.1'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}

✔ 我与 AndroidX 库没有冲突。

我应该使用片段来解决问题吗? 为什么会发生?

【问题讨论】:

    标签: android android-studio android-layout material-design android-toolbar


    【解决方案1】:

    这是由于工具栏中的默认内容插入。添加 工具栏中的 app:contentInsetStart="0dp" 和 app:contentInsetLeft="0dp"

    代码

     <androidx.appcompat.widget.Toolbar 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="?attr/actionBarSize"
            android:background="@color/colorPrimaryDark"
            android:elevation="10dp"
            app:contentInsetStart="0dp"
            app:contentInsetLeft="0dp">
            
            <!-- Your Child views-->
    
        </androidx.appcompat.widget.Toolbar>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 1970-01-01
      • 2016-09-20
      • 2020-03-23
      相关资源
      最近更新 更多