[英]Android Action Button not shown in action bar
I want to display an icon on the action bar. 我想在操作栏上显示一个图标。 But it always comes in the overflow list.
但它总是出现在溢出列表中。 Could you please help me up with a solution.
你能帮我解决一下吗? This is my code.
这是我的代码。
CreateScheduleActivity.java CreateScheduleActivity.java
package com.mpeers.ui;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class CreateScheduleActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_schedule);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
action_bar.xml in menu folder 菜单文件夹中的action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/plusicon"
android:title="CS"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
If you are using the appcompat support library, you need to define 如果您使用的是appcompat支持库,则需要定义
xmlns:app="http://schemas.android.com/apk/res-auto" 的xmlns:应用= “http://schemas.android.com/apk/res-auto”
in the parent tag and then use app:showAsAction for controlling the visibility of the action items. 在父标记中,然后使用app:showAsAction来控制操作项的可见性。
If you are not using the appcompat support library, you can use the android:showAsAction tag for controlling the visibility of the action items. 如果您不使用appcompat支持库,则可以使用android:showAsAction标记来控制操作项的可见性。
define xmlns:app="http://schemas.android.com/apk/res-auto"
in parent tag. 在父标记中定义
xmlns:app="http://schemas.android.com/apk/res-auto"
。
then use app:showAsAction="ifRoom"
然后使用
app:showAsAction="ifRoom"
Following is the example code. 以下是示例代码。
<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.taazi.android.MainActivity" >
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/action_right_drawer"
android:icon="@drawable/ic_action_play_over_video"
android:title="@string/action_right_drawer"
app:showAsAction="ifRoom"/>
</menu>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.