将工具栏用作 SupportActionBar 时菜单不会膨胀
Menu not inflating when using Toolbar as SupportActionBar
我在 activity 中有一个 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>
您将 showasaction 添加到 never 这就是它不会显示在操作栏中的原因。您需要像往常一样或根据您的使用情况添加 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>
我在 activity 中有一个 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>
您将 showasaction 添加到 never 这就是它不会显示在操作栏中的原因。您需要像往常一样或根据您的使用情况添加 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>