android - 工具栏布局与父宽度不匹配

标签 android android-layout android-actionbar android-toolbar

我已经阅读了几个类似的主题,但仍然没有找到此问题的答案。我有一个自定义的工具栏布局:

<android.support.design.widget.AppBarLayout    android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#99000000">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_transparent"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/MyToolbar">

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_centerInParent="true"
        android:id="@+id/toolbar_title" />

</RelativeLayout>

这是我的 Activity 代码:

    toolbar = (Toolbar) findViewById(R.id.toolbar_transparent);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

我的工具栏仍然有左右内边距,并且不填充父级宽度。我尝试过各种解决方案。我的代码有什么问题吗? 问题是我需要我的工具栏是半透明的。这就是为什么我在布局 android:background="#99000000"中使用这一行。我将它应用到整个布局,我的工具栏变得半透明,但它没有填充父级的宽度。但是,如果我将 android:background="#99000000"这条线不应用于整个布局,而是应用于布局中的工具栏小部件,它会正确填充父级,但会失去透明度。这有帮助吗?

最佳答案

如果填充是问题,您可以尝试设置 contentInset 属性:

https://developer.android.com/reference/android/widget/Toolbar.html#attr_android:contentInsetStart

关于android - 工具栏布局与父宽度不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43192795/

相关文章:

java - onOffsetsChanged : move Bitmap

java.lang.InstantiationException : class has no zero argument constructor error

android - 为什么底部导航状态不保存在撰写中?

android - 在 Android 上为自定义形状添加阴影

android - 在不同的 Activity 中使用 findviewbyid

android - 如何在 Activity 中显示后退按钮

android - 将 Camera SurfaceView 旋转为纵向

安卓.content.res.Resources$NotFoundException : String resource ID #0x12 - onLocationChanged()

android - 如何使用 AppCompat 从 Android 中的单独类添加 ActionBar?

带有两个拉伸(stretch)按钮的 Android 操作栏