Android工具栏布局宽度和高度是否自动更改?

Android工具栏布局宽度和高度是否自动更改?,android,android-layout,toolbar,android-toolbar,android-studio-2.3,Android,Android Layout,Toolbar,Android Toolbar,Android Studio 2.3,我是Android Studio的新手,当我在和活动中设置工具栏时,它给我带来了很多问题,问题是每当我将其布局宽度和高度更改为以下值时 布局\u宽度=匹配\u父项 layout\u height=attr/actionBarsize 它正在返回到以下内容 布局\u宽度=368dp 布局高度=56dp 无论它尝试更改多少次,它都会一次又一次地给我相同的结果。XML: <RelativeLayout xmlns:android="http://schemas.android.com/apk/r

我是Android Studio的新手,当我在和活动中设置工具栏时,它给我带来了很多问题,问题是每当我将其布局宽度和高度更改为以下值时

布局\u宽度=匹配\u父项
layout\u height=attr/actionBarsize

它正在返回到以下内容

布局\u宽度=368dp
布局高度=56dp

无论它尝试更改多少次,它都会一次又一次地给我相同的结果。

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:id="@+id/coach_bar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#F4B702"
        app:popupTheme="@style/AppTheme.PopupOverlay" >

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

 <!--<Your Design>-->

</RelativeLayout >
Manifest.xml

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar"/>

是的,这也让我很恼火。我猜想您正在尝试将工具栏放在约束窗口中

ConstraintLayout不支持match_parent,它希望约束每个视图。当您在Android Studio的设计模式中单击时,编辑器将启动并尝试“修复”xml的任何问题。在这种情况下,它会发现您的工具栏未受约束,并且未正确使用match_parent,因此会尝试为您修复此问题(您应该会看到工具栏未受约束的警告)

我认为你有两个选择:

1在布局中约束工具栏,并将“匹配父项”替换为“0dp”

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="0dp"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:layout_marginLeft="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="0dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="0dp" />

2将工具栏置于协调器布局内。您以前必须使用工具栏来实现滚动功能,但对于新的ConstraintLayout,我不确定这是否仍然适用

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.CoordinatorLayout>


我遇到了这个问题,最后我发现更改后我没有保存style.xml

将您的代码放在这里
layout\u hight
有一个打字错误-确保这不是您代码中的问题。请学习如何使用此处的块格式工具-您的代码块可以使用标记代码进行格式设置按钮。@halfer你能再详细说明一下吗
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.CoordinatorLayout>