1

我已阅读了几个类似主题,但仍未找到针对此问题的答案。我有一个自定义布局工具栏:工具栏布局与父宽度不匹配

<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 =“#99000000”中使用此行。我将其应用于整个布局,并且我的工具栏变为半透明,但它不会在宽度上填充父项。但是,如果我应用android:background =“#99000000”这行不是为了整个布局,而是在布局中的我的工具栏小部件中,它正确填充了父项,但是失去了透明度。这可以帮助吗?

+0

你的主题MyToolbar里面有什么? – Yupi

+0

@Yupi <样式名称= “MyToolbar” 父= “ThemeOverlay.AppCompat.Dark.ActionBar”> <项目名称= “机器人:homeAsUpIndicator”> @绘制/ BACK_ARROW – fregomene

+0

您可以手动尝试设置填充是以防万一。 –

回答

1

试试这个布局:

半Transperant toolbar

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/general_appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@null"> 

    <android.support.v7.widget.Toolbar 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="#99000000"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:text="Login" 
      android:textSize="20sp" /> 
    </android.support.v7.widget.Toolbar> 

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

输出:

enter image description here

+0

谢谢你的回答,但问题仍未解决:( – fregomene

+0

@fregomene我编辑了答案... – rafsanahmad007

+0

左右填充保持不变。我的布局也是半透明的,但我无法处理填充( – fregomene