Android 在片段布局的constraintLayout中添加工具栏
我试图在约束布局中添加工具栏,但得到的结果是: 正如你所看到的,它不适合整个屏幕。 以下是我的xml:Android 在片段布局的constraintLayout中添加工具栏,android,xml,layout,toolbar,android-constraintlayout,Android,Xml,Layout,Toolbar,Android Constraintlayout,我试图在约束布局中添加工具栏,但得到的结果是: 正如你所看到的,它不适合整个屏幕。 以下是我的xml: <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://schem
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment title"/>
</android.support.v7.widget.Toolbar>
</android.support.constraint.ConstraintLayout>
ConstraintLayout宽度和长度设置为匹配父级(哪个应该是屏幕?),工具栏宽度设置为匹配布局宽度。
我遗漏了什么?在工具栏中添加这些内容,试试看
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp"
这是我从你那里得到的。如果这不能解决您的问题,您也可以尝试其他解决方案。尝试此方法您必须将constraintTop设置为父级,同时使宽度值与约束匹配,并将start和end约束设置为父级 检查下面的代码
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment title" />
</android.support.v7.widget.Toolbar>
</android.support.constraint.ConstraintLayout>
我通过将操作栏放在主活动上而不是片段布局上解决了此问题。检查此问题的答案尝试添加自定义工具栏?自动添加到片段中的工具栏如果添加了自定义视图,则创建自定义视图文件这是活动xml或片段xml?这是片段的布局这没有更改什么都可以,这没用。没有更改。因此,如果使用片段,请检查框架布局或片段视图在活动XML上是否没有任何填充或边距