无法将按钮添加到 Android ActionBar API 21
Can't add Button to Android ActionBar API 21
我想在我的 Android ActionBar 中添加一个搜索按钮,为此我遵循了 this 的答案。唯一发生的事情是 "Search" 按钮被添加到菜单本身而不是 ActionBar(见下面的屏幕截图)。
这是我的search.java(显示搜索按钮的Activity):
public class search extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
return super.onCreateOptionsMenu(menu);
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的 menu_search.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title="search"
android:id="@+id/action_search"
android:icon="@drawable/searchicon"
android:orderInCategory="100"
android:showAsAction="always"/>
</menu>
感谢任何帮助!提前致谢!
您正在使用 appcompat-v7
。您需要将 android:showAsAction
属性更改为 app:showAsAction
属性,并定义 app
命名空间 (xmlns:app="http://schemas.android.com/apk/res-auto"
).
这会给你这样的东西:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="search"
android:id="@+id/action_search"
android:icon="@drawable/searchicon"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>
此外,您可能不需要 android:orderInCategory
。
你应该做的是添加:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_search"
android:title="search"
app:showAsAction="always"
android:actionViewClass="android.widget.SearchView"/>
我想在我的 Android ActionBar 中添加一个搜索按钮,为此我遵循了 this 的答案。唯一发生的事情是 "Search" 按钮被添加到菜单本身而不是 ActionBar(见下面的屏幕截图)。
这是我的search.java(显示搜索按钮的Activity):
public class search extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
return super.onCreateOptionsMenu(menu);
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的 menu_search.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title="search"
android:id="@+id/action_search"
android:icon="@drawable/searchicon"
android:orderInCategory="100"
android:showAsAction="always"/>
</menu>
感谢任何帮助!提前致谢!
您正在使用 appcompat-v7
。您需要将 android:showAsAction
属性更改为 app:showAsAction
属性,并定义 app
命名空间 (xmlns:app="http://schemas.android.com/apk/res-auto"
).
这会给你这样的东西:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="search"
android:id="@+id/action_search"
android:icon="@drawable/searchicon"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>
此外,您可能不需要 android:orderInCategory
。
你应该做的是添加:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_search"
android:title="search"
app:showAsAction="always"
android:actionViewClass="android.widget.SearchView"/>