Skip to content Skip to sidebar Skip to footer

Manually Inflating Custom View Yields Different Layouts For Actionbar Custom View

Custom view from resource: // Set up the action bar. final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); actionBar.setCus

Solution 1:

Actually, the issue here is that in second case ActionBar needs additonal layout parameters:

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

So, it covers all ActionBar area. Looks like by default WRAP_CONTENT llayout parameters get applied to custom view.

Solution 2:

Another options is to let the ActionBar do the work for you (in this case I'm showing how to do it with the support version).

// Set your custom view
getSupportActionBar().setCustomView(R.layout.custom_action_bar);

// Get the inflated viewViewview= getSupportActionBar().getCustomView();

// Do what you want with the view (set the title, custom font etc.)TextViewactionBarTitle= (TextView) view.findViewById(R.id.action_bar_title);
...

// set the custom flag
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)

Post a Comment for "Manually Inflating Custom View Yields Different Layouts For Actionbar Custom View"