Why doesn't my custom action bar display match parent when using appcompat and toolbar?

I customize the view in the action bar. It fills the width of the action bar when using the SDK action bar, but not when using the appcompat version. Why doesn't my line layout fill the width when using appcompat and toolbar? And what can I do with it?

Here it looks like pre-appcompat:

enter image description here

And here is how it looks with appcompat. (The red color indicates the size of my custom view.):

enter image description here

Edit: Here is what it looks like using the params @Seidan hint. (Shows black text where it should be white):

enter image description here

Here is my custom layout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:background="#800" > <FrameLayout android:id="@+id/actionbar_discard" style="?attr/actionButtonStyle" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <TextView style="?android:actionBarTabTextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:drawableLeft="@drawable/ic_close_white_24dp" android:drawablePadding="8dp" android:gravity="center_vertical" android:paddingRight="20dp" android:text="@string/actionbar_cancel" /> </FrameLayout> <FrameLayout android:id="@+id/actionbar_done" style="?attr/actionButtonStyle" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <TextView style="?android:actionBarTabTextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:drawableLeft="@drawable/ic_check_white_24dp" android:drawablePadding="8dp" android:gravity="center_vertical" android:paddingRight="20dp" android:text="@string/actionbar_done" /> </FrameLayout> </LinearLayout> 

And this is how I customize the action bar:

 ActionBar ab = getSupportActionBar(); ab.setDisplayHomeAsUpEnabled(false); ab.setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE); ab.setCustomView(R.layout.actionbar_discard_done); 

Both appcompat-v7: 22.0.0 and the new appcompat-v7: 22.1.0 demonstrate this problem.

+6
source share
2 answers

Thanks @Seidan for your comment. This led me to a decision. A design problem arose because the cheater used the wrong theme when I gave it my activity.

To summarize, instead of this line from my code in the question ...

 ab.setCustomView(R.layout.actionbar_discard_done); 

... I have...

 View v = LayoutInflater .from(ab.getThemedContext()) .inflate(R.layout.actionbar_discard_done, null); ActionBar.LayoutParams params = new ActionBar.LayoutParams( ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT); ab.setCustomView(v, params); 

What gives me ...

correct action bar

It's a mystery why we should specify match_parent in layout and code, but I can live with it.

+8
source

In onCreate

 RelativeLayout relativeLayout_custombar = (RelativeLayout)findViewById(R.id.relativeLayout_custombar); Display display = getWindowManager().getDefaultDisplay(); int x_size = display.getWidth(); LayoutInflater mInflater = LayoutInflater.from(this); View mCustomView = mInflater.inflate(R.layout.your_customview, null); getSupportActionBar().setCustomView(mCustomView); getSupportActionBar().setDisplayShowCustomEnabled(true); Toolbar parent = (Toolbar) mCustomView.getParent(); parent.setContentInsetsAbsolute(0, 0); parent.findViewById(R.id.relativeLayout_custombar).getLayoutParams().width = x_size; 
+1
source

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


All Articles