Custom Home Icon in Action Bar Sherlock

2019-03-12 06:08发布

问题:

I am trying to set custom icon for home icon using ActionBarSherlock library. I have tried to set custom layout using abHomeLayout attribute in my custom theme. But it didn't work for me. Only way, how to set it, is to set abIcon attribute for my custom drawble, but I cannot set some horizontal padding for this drawable. Is there any example for this or where could be a problem with abHomeLayout attribute?

回答1:

This works for my situation, it replaces the default ic_launcher icon in the action bar with my custom one.

In your onCreate do this:

getSupportActionBar().setIcon(R.drawable.audio_shortcut);

Or in the manifest you can set the logo:

<activity>
    android:logo="@drawable/my_custom_logo"
    ...
</activity>


回答2:

This works great too:

<style name="Theme.Styled" parent="Theme.Sherlock.Light.DarkActionBar">
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
</style>

<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
    <item name="icon">@drawable/my_custom_logo</item>
    <item name="android:icon">@drawable/my_custom_logo</item>
</style>

Reference: How to change the ActionBar “Home” Icon to be something other than the app icon?



回答3:

I had similar issue with incorrect padding of the home icon on devices api<11 (i.e not the fully fledged platform action bar) and the abHomeLayout style only worked api>11

My solution was to copy the abs__action_bar_home.xml layout from ABS to child project and manually add padding to the abs__home imageview

<ImageView
    android:id="@+id/abs__home"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:paddingTop="@dimen/action_bar_home_icon_top_bottom_padding"
    android:paddingBottom="@dimen/action_bar_home_icon_top_bottom_padding"
    android:paddingLeft="@dimen/action_bar_home_icon_left_right_padding"
    android:paddingRight="@dimen/action_bar_home_icon_left_right_padding"
    android:scaleType="centerInside" />


回答4:

Define an own style, like this:

<resources>
  <style name="Theme.OwnTheme" parent="@style/Theme.Sherlock">
    <item name="abBackground">#476dd5</item>
    <item name="abHeight">30dip</item>
  </style>
</resources>

I think here your can use abHomeLayout. Set this style for your activity like this:

<activity 
 android:name=".MainActivity" 
 android:theme="@style/Theme.OwnTheme">
   <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
</activity>

In MainActivity you can use getSupportActionBar()... functions.