Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.setCustomView(R.layout.custom_action_bar);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

the result is:
enter image description here

Custom view manually inflated:

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

the result is:
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 the correct one, because it has weights on its widgets. The second attempt should produce the same result, but it does not.

like image 863
ilomambo Avatar asked Jul 08 '13 08:07

ilomambo


People also ask

What does inflating a layout mean?

"Inflating" a view means taking the layout XML and parsing it to create the view and viewgroup objects from the elements and their attributes specified within, and then adding the hierarchy of those views and viewgroups to the parent ViewGroup.

What does inflating a view do?

An inflate process will: read a layout XML. parsing it. and making Java View object to create the UI (ie viewgroup for container and views for widget)

Which view can be used to customize action bar?

Custom Action Bar Layout The view layout consists of two ImageButtons that represent forward and back image buttons and a TextView in the center.

How to set custom ActionBar in android?

This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

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

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

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

like image 90
sandrstar Avatar answered Oct 06 '22 00:10

sandrstar