我的代码出了什么问题,它无法显示我的任何Android ActionBar图标。 以下是我的代码:纠正错过的地方。
我的style.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
我的themes.xml:我使用了自定义主题。我想从这里我需要更改为显示图标
<?xml version="1.0" encoding="utf-8"?>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:actionMenuTextColor">@style/MyActionBarTitleText</item>
<item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@color/PrimaryColor</item>
</style>
<!-- ActionBar title text -->
<style name="MyActionBarTitleText"
parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:background">@color/ActionBarTitleText</item>
</style>
<!-- ActionBar tabs text styles -->
<style name="MyActionBarTabText"
parent="@android:style/Widget.Holo.ActionBar.TabText">
<item name="android:textColor">@color/ActionBarTitleText</item>
</style>
MainActivity:
//ActionBar creating/adding icon in Menu
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_activity_menu, menu);
return super.onCreateOptionsMenu(menu);
}
//ActionBar Menu icon listerner like clicking options
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.info:
Toast.makeText(getApplicationContext(),
"Info Selected",Toast.LENGTH_SHORT).show();
case R.id.st:
Toast.makeText(getApplicationContext(),
"Setting Selected",Toast.LENGTH_SHORT).show();
default:
return super.onOptionsItemSelected(item);
}
}
main_activity_menu.xml:我试过“总是”&amp; “IfRoom”但它不起作用。
<?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"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id = "@+id/info"
android:title="info"
android:icon="@drawable/ic_info_outline_white_24dp"
app:showAsAction="always|withText">
</item>
<item
android:id = "@+id/st"
android:title="settings"
android:icon="@drawable/ic_settings_white_24dp"
app:showAsAction="always">
</item>
答案 0 :(得分:2)
您应该使用android:showAsAction = "ifRoom"
答案 1 :(得分:1)
如果您使用AppCompatActivity,请尝试this
将此代码添加到您的Activity类:
@Override
protected boolean onPrepareOptionsPanel(View view, Menu menu) {
if (menu != null) {
if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
try {
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "onMenuOpened...unable to set icons for overflow menu", e);
}
}
}
return super.onPrepareOptionsPanel(view, menu);
}
答案 2 :(得分:0)
你最好在onCreateOptionsMenu中返回true 。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_activity_menu, menu);
return true;
}
除此之外,您的代码中似乎还有其他一些问题。例如,你应该把断裂放在每个案例句子的尾部。
case R.id.info:
Toast.makeText(getApplicationContext(),
"Info Selected",Toast.LENGTH_SHORT).show();
break;
case R.id.st:
Toast.makeText(getApplicationContext(),
"Setting Selected",Toast.LENGTH_SHORT).show();
break;
答案 3 :(得分:0)
尝试以下,
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
答案 4 :(得分:0)
您只需将app:showAsAction
更改为android:showAsAction
,因为您没有使用兼容性主题