GetSupportActionBar (). setCustomView (view) does not fill the entire action bar

I am trying to customize a custom view for my application, and the layout I made does not fill the entire action bar. I tried so many style settings, but no one worked for me.

here is my code in action

View view = getLayoutInflater().inflate(R.layout.actionbar_customized_home, null); getSupportActionBar().setDisplayShowCustomEnabled(true); getSupportActionBar().setCustomView(view); 

here is the layout placed as view

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/actionbar_color" > ![enter image description here][1] <ImageView android:layout_width="wrap_content" android:layout_height="25dp" android:layout_centerInParent="true" android:scaleType="fitCenter" android:src="@drawable/logo"/> </RelativeLayout> 

Even against the background of menu items such as navdrawer, how can I make my custom view as the background color?

I really appreciate the help

+9
source share
4 answers

Define your main container as follows:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_gravity="fill_horizontal" android:background="@color/actionbar_color" > <ImageView android:layout_width="wrap_content" android:layout_height="25dp" android:layout_centerInParent="true" android:scaleType="fitCenter" android:src="@drawable/logo"/> </RelativeLayout> 

The important part is android:layout_gravity="fill_horizontal , which should solve your problem.

EDIT:

if this does not work, try to do this:

 View view = getLayoutInflater().inflate(R.layout.actionbar_customized_home, null); LayoutParams layout = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); getSupportActionBar().setCustomView(view, layout); 
+8
source

This is all you need to add to your onCreate()

 getSupportActionBar().setCustomView(new CustomView(this)); getSupportActionBar().setDisplayShowCustomEnabled(true); 
+20
source

Custom views occupy only the area between the nav / up button and the overflow menu. To change the background and other colors see this https://developer.android.com/training/basics/actionbar/styling.html

0
source
 ActionBar.LayoutParams layout = new ActionBar.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View viewCustom = inflator.inflate(R.layout.custom_searchview_actionbar, null); ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setCustomView(viewCustom, layout); actionBar.setDisplayShowCustomEnabled(true); Log.i("SEARCH", "ACTIONBAR IS NOT NULL"); } else Log.i("SEARCH", "ACTIONBAR IS NULL :(("); 
0
source

Source: https://habr.com/ru/post/1208199/


All Articles