我正在尝试向action item
添加action bar
,但未显示操作项的图标。
XML文件res / menu / actionbar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="test"
android:showAsAction="always" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />"
</menu>
添加action bar
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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.actionbar, 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();
switch(id)
{
case R.id.action_search:
Context context = getApplicationContext();
CharSequence text = "Action Button Pressed!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
我将minimum sdk
中的manifest file
设置为7
<uses-sdk android:minSdkVersion="7" />
答案 0 :(得分:1)
尝试使用菜单xml
<?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:icon="@drawable/ic_action_search"
android:title="test"
app:showAsAction="always" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never" />"
</menu>