安卓工具栏布局宽度和高度自动更改



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

layout_width=match_parent
layout_height=attr/actionbarsize

它正在恢复更改为以下内容

layout_width=368dp
layout_height=56dp

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

是的,这也让我很烦。我假设您正在尝试将工具栏放在约束布局中。

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

我认为您有 2 个选择:

1 约束布局中的工具栏并将"match_parent"替换为"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 将工具栏放在协调器布局中。您曾经必须这样做才能使用工具栏滚动功能,但是对于新的约束布局,我不确定这是否仍然适用

<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>

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 >

爪哇岛

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setLogo(R.drawable.my_icon);
setSupportActionBar(toolbar);
if(getSupportActionBar()!=null)
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);

清单.xml

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

我遇到了这个问题,最后我发现我没有保存样式.xml在它改变之后。

最新更新