Manually inflate a custom view gives different layouts for a custom ActionBar view

Custom view from the resource:

// Set up the action bar. final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); actionBar.setCustomView(R.layout.custom_action_bar); actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 

result:
enter image description here

User view manually inflates:

 // Set up the action bar. final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); LayoutInflater inflater = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom_action_bar, null); actionBar.setCustomView(view); actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 

result:
enter image description here

custom_action_bar.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="3"> <TextView android:id="@+id/bar_title1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@color/White" android:text="title1"/> <TextView android:id="@+id/bar_title2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@color/White" android:text="title2"/> <TextView android:id="@+id/bar_title3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@color/White" android:text="title3"/> </LinearLayout> 

The first layout shown here is correct because it has weights in its widgets. The second attempt should give the same result, but it is not.

+6
source share
2 answers

Actually, the problem is that in the second case, the ActionBar needs additional layout options:

  final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom_action_bar, null); actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 

Thus, it covers the entire scope of the ActionBar. It appears that, by default, WRAP_CONTENT parameters apply to the user view.

+14
source

Other options are to let ActionBar do the job for you (in this case, I'll show you how to do it with support).

 // Set your custom view getSupportActionBar().setCustomView(R.layout.custom_action_bar); // Get the inflated view View view = getSupportActionBar().getCustomView(); // Do what you want with the view (set the title, custom font etc.) TextView actionBarTitle = (TextView) view.findViewById(R.id.action_bar_title); ... // set the custom flag getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM) 
+9
source

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


All Articles