4

ツールバーをアクション バーとして使用しようとしており、Chris Banes ガイドに従っています。

https://chris.banes.me/2014/10/17/appcompat-v21/

そのセットアップに従って、空のツールバーができました。getMenuInflater().inflate() が機能しないようです。

空のツールバー

私の活動では:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menuhome, menu);
        [...]

そして onCreate() で:

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

そして、これは私のレイアウトです:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:allmytv="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/linearlayoutmain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v7.widget.Toolbar
        xmlns:allmytv="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary" />

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        allmytv:pstsIndicatorColor="#f57c1d" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </android.support.v4.view.ViewPager>


</LinearLayout>

どこが間違っていますか?

4

2 に答える 2

5

ツールバーをアクションバーとして使用するには、windowActionBar 属性を false に設定する必要があります。

styles.xml に以下を含めます。

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item> 
        <item name="colorPrimary">#4285F6</item>
    </style>

タグの下のmanifest.xmlで、上記のテーマを使用します

android:theme="@style/AppCompatTheme"

メニューを膨らませるためにツールバーを使用しました:

 toolbar.inflateMenu(R.menu.your_toolbar_menu);
于 2014-11-13T05:09:34.477 に答える