操作栏背景大小,Android

Action Bar Background size, Android

我想将操作栏背景颜色更改为自定义颜色。这是我目前得到的:

如何在整个操作栏宽度上获得这种颜色?

我的操作栏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);

请在末尾添加这一行。

工具栏父级=(工具栏)actionBar.getParent(); parent.setContentInsetsAbsolute(0,0);

如果你正在设计你的自定义 actionBar,而不是像这样编写你的代码:

  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);

希望对您有所帮助!