使用工具栏作为SupportActionBar时菜单不膨胀

时间:2016-01-04 05:11:47

标签: android android-actionbar android-toolbar

我在活动中获得了Toolbar,并且我已将其声明为Support ActionBar。我试图给我的菜单充气,但它并没有显示出来。相反,只有一个设置图标(三个垂直点)出现在右侧角落。这是一些代码:

PoliticianPage.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_politician_page);
    mTitle = getTitle();

    // Set up the drawer.

    Toolbar mToolbar = (Toolbar) findViewById(R.id.politicianToolbar);

    setSupportActionBar(mToolbar);
    mTitle="Barack Obama";
    restoreActionBar();

    // Create a new Fragment to be placed in the activity layout
    PoliticianNewsAndUpdatesPreview newsAndUpdates = new PoliticianNewsAndUpdatesPreview();
    // Add the fragment to the 'fragment_container' FrameLayout
    getSupportFragmentManager().beginTransaction().add(R.id.politicianFragmentSwitcher, newsAndUpdates).commit();


}
public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_politician_page, menu);
    return super.onCreateOptionsMenu(menu);
}

activity_politician_page.xml

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/tools"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:id="@+id/politicianToolbar"
            app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            >
        </android.support.v7.widget.Toolbar>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <include android:id="@+id/politicianHeader" layout="@layout/politician_header" />
            <include android:id="@+id/politicianLinks" layout="@layout/politician_links"
                android:layout_marginTop="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

            <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/politicianFragmentSwitcher"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_height="48dp"
                    android:layout_width="match_parent"
                    android:paddingStart="16dp"
                    android:layout_marginTop="10dp">

                    <ImageView
                        android:layout_width="24dp"
                        android:layout_height="24dp"
                        android:layout_marginEnd="16dp"
                        android:layout_gravity="center_vertical"
                        android:src="@drawable/ic_attach_money_black_24dp"
                        />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="16dp"
                        android:layout_marginStart="16dp"
                        android:layout_marginRight="16dp"
                        android:drawablePadding="16dp"
                        android:layout_marginEnd="16dp"
                        android:textSize="14sp"
                        android:alpha="1"
                        android:gravity="center_vertical"
                        android:textColor="#000000"
                        android:fontFamily="sans-serif-medium"
                        android:textStyle="normal"
                        android:text="Donate"
                        />
                </LinearLayout>

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_height="48dp"
                    android:layout_width="match_parent"
                    android:paddingStart="16dp">

                    <ImageView
                        android:layout_width="24dp"
                        android:layout_height="24dp"
                        android:layout_marginEnd="16dp"
                        android:layout_gravity="center_vertical"
                        android:src="@drawable/ic_email_black_24dp"

                        />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="16dp"
                        android:layout_marginStart="16dp"
                        android:layout_marginRight="16dp"
                        android:drawablePadding="16dp"
                        android:layout_marginEnd="16dp"
                        android:textSize="14sp"
                        android:alpha="1"
                        android:gravity="center_vertical"
                        android:textColor="#000000"
                        android:fontFamily="sans-serif-medium"
                        android:textStyle="normal"
                        android:text="Send Email"

                        />
                </LinearLayout>

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_height="48dp"
                    android:layout_width="match_parent"
                    android:paddingStart="16dp">

                    <ImageView
                        android:layout_width="24dp"
                        android:layout_height="24dp"
                        android:layout_marginEnd="16dp"
                        android:layout_gravity="center_vertical"
                        android:src="@drawable/ic_globe_black_24dp"

                        />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="16dp"
                        android:layout_marginStart="16dp"
                        android:layout_marginRight="16dp"
                        android:drawablePadding="16dp"
                        android:layout_marginEnd="16dp"
                        android:textSize="14sp"
                        android:alpha="1"
                        android:gravity="center_vertical"
                        android:textColor="#000000"
                        android:fontFamily="sans-serif-medium"
                        android:textStyle="normal"
                        android:text="Visit Website"

                        />
                </LinearLayout>


            </LinearLayout>

            <include layout="@layout/horizontal_line" />
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_height="48dp"
                android:layout_width="match_parent"
                android:paddingStart="16dp">

                <ImageView
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:layout_marginEnd="16dp"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/ic_domain_black_24dp"

                    />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="16dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginRight="16dp"
                    android:drawablePadding="16dp"
                    android:layout_marginEnd="16dp"
                    android:textSize="14sp"
                    android:alpha="1"
                    android:gravity="center_vertical"
                    android:textColor="#000000"
                    android:fontFamily="sans-serif-medium"
                    android:textStyle="normal"
                    android:text="Legal / About Us"

                    />
            </LinearLayout>

        </LinearLayout>


    </LinearLayout>

</ScrollView>

menu_politician_page.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context="com.politicsappv2.PoliticianPage">
    <item android:icon="@drawable/ic_message_white_24dp"
        android:id="@+id/action_community" android:title="Community"
        android:orderInCategory="100" app:showAsAction="never" />
    <item android:icon="@drawable/ic_bookmark_outline_white_24dp"
        android:id="@+id/action_bookmark" android:title="Bookmark"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

1 个答案:

答案 0 :(得分:2)

你添加showasaction从来没有这就是为什么它不会显示在操作栏中。你需要一如既往地或根据你的用法添加showAsAction,请阅读以下showAsAction参数。

http://developer.android.com/guide/topics/resources/menu-resource.html

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context="com.politicsappv2.PoliticianPage">
    <item android:icon="@drawable/ic_message_white_24dp"
        android:id="@+id/action_community" android:title="Community"
        android:orderInCategory="100" app:showAsAction="always" />
    <item android:icon="@drawable/ic_bookmark_outline_white_24dp"
        android:id="@+id/action_bookmark" android:title="Bookmark"
        android:orderInCategory="100" app:showAsAction="ifRoom" />
</menu>