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