App compat actionbar v21 app icon is not showing

2020-03-30 01:25发布

I upgraded to avtionbar app compat v21 for material design purpose.

But I observed that its not showing the app icon in actionbar and navigation menu and back button is taking more space compare to old appcompat libs.

Have any one faced this issue? I searched a lot but did not found anything useful.

Below line is also not working.

getSupportActionBar().setLogo(R.drawable.ic_launcher);

I am using style for actionbar which is like

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:icon">@drawable/ic_action_bar</item>
        <item name="android:background">@drawable/action_bar_bg</item>
        <item name="android:windowContentOverlay">@null</item>
        <!-- Support library compatibility -->
        <item name="background">@drawable/action_bar_bg</item>
</style>

I am aware of other solution like Toolbar. But I am in middle of release and due to time constraint I am looking for some quick solution if available. Adding a Toolbar will require more time.

3条回答
叼着烟拽天下
2楼-- · 2020-03-30 01:49

setIcon/setLogo method will only work if you have set DisplayOptions Try this -

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setIcon(R.drawable.ic_launcher);

You can also set options for displaying LOGO(just add constant ActionBar.DISPLAY_USE_LOGO). More information - displayOptions

查看更多
倾城 Initia
3楼-- · 2020-03-30 01:57

Use getSupportActionbar instead of actionbar

Actionbar actionbar = getSupportActionBar()
actionbar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionbar.setIcon(YOUR ICON);
查看更多
Evening l夕情丶
4楼-- · 2020-03-30 01:58

I added this to my theme:

<item name="android:windowNoTitle">false</item>
<item name="android:windowActionBar">true</item>
<item name="windowActionBar">true</item>

and then the ActionBar was back.

Note: if you use supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); then you can't just do that, but will have to add the Toolbar to your layout (which isnt that much work, anyway)

查看更多
登录 后发表回答