简体   繁体   中英

Action Bar Background size, Android

I want to change the action bar background color to a custom color. This is what I currently get:

在此处输入图片说明

How do I get this color on the full action bar width?

My action bar 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 method:

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

please add this line at the end .

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

If you are designing your custom actionBar than write your code like this:

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

Hope it helps !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM