工具栏不允许在其中使用匹配父布局



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

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

👉你可以在这张照片中看到。

<?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(模块:应用程序(:

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库没有冲突。

我可以用碎片来解决这个问题吗?为什么会发生这种情况?

这是由于工具栏中插入了默认内容。添加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>

最新更新