手动膨胀自定义视图会为ActionBar自定义视图生成不同的布局

时间:2013-07-08 08:07:39

标签: android android-actionbar android-custom-view android-inflate

来自资源的自定义视图:

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

结果是:
enter image description here

自定义视图手动充气:

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

结果是:
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>

此处显示的第一个布局是正确的布局,因为它的小部件上有权重。第二次尝试应该产生相同的结果,但事实并非如此。

2 个答案:

答案 0 :(得分:13)

实际上,这里的问题是,在第二种情况下,ActionBar需要额外的布局参数:

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

因此,它涵盖了所有ActionBar区域。默认情况下看起来WRAP_CONTENT llayout参数会应用于自定义视图。

答案 1 :(得分:9)

另一个选择是让ActionBar为您完成工作(在这种情况下,我将展示如何使用支持版本)。

// 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)
相关问题