2015-06-02 79 views
1

我想將動作欄背景顏色更改爲自定義顏色。這是我目前得到:Action Bar背景大小,Android

enter image description here

如何獲得在全操作欄寬這個顏色?

我的行動吧xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:background="#0c555c" 
    android:gravity="center" 
    android:id="@+id/action_bar_title" 
    android:paddingEnd="0dp" 
    android:paddingStart="0dp" 
    android:showAsAction="ifRoom"> 
</RelativeLayout> 

onCreate方法:

RelativeLayout actionBar = (RelativeLayout)findViewById(R.id.action_bar_title); 
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.action_bar); 

回答

0

如果您設計您的自定義動作條不是寫你這樣的代碼:

ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayShowCustomEnabled(true); 
    actionBar.setDisplayShowTitleEnabled(false); 
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8B58A3"))); 

    LayoutInflater inflater = LayoutInflater.from(this); 
    @SuppressLint("InflateParams") 
    View v = inflater.inflate(R.layout.titleview, null); 

    actionBar.setCustomView(v); 

希望它能幫助!

+0

什麼意思@SuppressLint(「InflateParams」)? 感謝它的工作! – Ernis

+0

Ernis由於null的存在而被傳遞,這意味着「在對佈局進行膨脹時,請避免作爲父視圖傳遞null,否則將忽略所有膨脹佈局的根部佈局參數。」 所以要忽略這個警告我們通過@suppressLint據我所知 – Umair

+0

感謝您的解釋! – Ernis

0

請在末尾加上這一行。

Toolbar parent =(Toolbar)actionBar.getParent(); parent.setContentInsetsAbsolute(0,0);

+0

什麼是視圖?因爲我沒有在你身邊使用那個 – Ernis

+0

它是actionBar,你在這裏受到侵害的佈局文件。 – Suraj

+0

mhm ..即時通訊做錯了什麼,因爲當我在最後輸入該代碼時,我得到一個新的錯誤與java.lang.NullPointerException – Ernis