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

时间:2017-04-03 19:29:36

标签: 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>

这是我的活动代码:

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

我的工具栏仍然有左右填充,并且不会填充父宽度。我尝试了各种解决方案。我的代码出了什么问题? 问题是我需要我的工具栏是半透明的。这就是我在布局android中使用这一行的原因:background =&#34;#99000000&#34;。我将它应用于整个布局,我的工具栏变为半透明,但它不会在宽度上填充父级。但如果我应用android:background =&#34;#99000000&#34;这一行不是整个布局,而是布局中我的工具栏小部件正确填充父级,但失去了透明度。这可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

试试这个布局:

for semi Transperant os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings.production"

./manage.py runserver --settings=project_name.settings.local

输出:

enter image description here

相关问题