ActionBar / ActionBarSherlock中的自定义图像而不是徽标

时间:2013-10-04 02:38:41

标签: android image android-actionbar actionbarsherlock graphical-logo

如何将ActionBar的默认徽标图标更改为自定义图像?类似于它在Whatsapp上的工作方式? Whatsapp custom image as logo

4 个答案:

答案 0 :(得分:3)

如果提供了清单,则ActionBar会使用清单的android:logo属性。这使您可以为图标(Launcher)和徽标(ActionBar等)使用单独的可绘制资源。


所以你应该将这个标签添加到清单中,如..

<application
    android:logo="@drawable/custom_image"

更新:

您可以将ActionBar.setLogo()用于运行时。有两个版本setLogo(int resId)setLogo(Drawable logo)

阅读Define custom Logo for ActionBar (different than Logo) in XML?,这也有助于您定义一些样式。

答案 1 :(得分:1)

最简单的方法是使用setIcon(R.drawable.icon_name)同样可以使用标题,也可以在操作栏字幕中设置日期。

ActionBar ab= getActionBar();
ab.setTitle("Aries");
ab.setSubtitle(dateFormat.format(date));
ab.setIcon(R.drawable.aries3d);

答案 2 :(得分:0)

您需要自定义ActionBarSherlock。在values文件夹中包含一个名为“styles_mytheme.xml”的xml:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="Theme.Styled" parent="@style/Theme.Sherlock.Light">
        <item name="actionBarStyle">@style/CustomActionBarStyle</item>
        <item name="android:actionBarStyle">@style/CustomActionBarStyle</item>
        <item name="android:actionBarItemBackground">@drawable/overflow_selector</item>
        <item name="android:itemBackground">@drawable/overflow_selector</item>
        <item name="android:actionBarWidgetTheme">@style/CustomActionOverflowDropDownText</item>
        <item name="actionBarWidgetTheme">@style/CustomTitleColorBar</item>
        <item name="android:icon">@drawable/ic_nav_home_back</item>
        <item name="android:homeAsUpIndicator">@drawable/ic_nav_back</item>
        <item name="icon">@drawable/ic_nav_home_back</item>
        <item name="dropDownListViewStyle">@style/DropDownStyles</item>
        <item name="android:dropDownListViewStyle">@style/DropDownStyles</item>
    </style>

    <style name="CustomActionBarStyle" parent="@style/Widget.Sherlock.ActionBar">

        <!-- define background for action bar (sets default for all parts of action bar - main, stacked, split) -->
        <item name="android:background">@drawable/navigation</item>
        <item name="background">@drawable/navigation</item>
        <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
        <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
    </style>

    <!-- Style for the color title bar -->
    <style name="CustomTitleColorBar" parent="@style/TextAppearance.Sherlock.Widget.ActionBar.Title">
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">12sp</item>
    </style>

    <style name="CustomActionOverflowDropDownText" parent="Widget">
        <item name="android:textColor">@color/white</item>
    </style>

    <style name="MyTheme.ActionBar.TitleTextStyle" parent="@style/TextAppearance.Sherlock.Widget.ActionBar.Title">
        <item name="android:textColor">@color/white</item>
    </style>

    <style name="My_Theme" parent="Theme.Sherlock">
        <item name="android:popupMenuStyle">@style/MyApp.PopupMenuStyle</item>
        <item name="popupMenuStyle">@style/MyApp.PopupMenuStyle</item>
    </style>

    <style name="MyApp.PopupMenuStyle" parent="Widget.Sherlock.ListPopupWindow">
        <item name="android:popupBackground">@drawable/navigation</item>
    </style>

    <style name="DropDownStyles" parent="Widget.Sherlock.ListView.DropDown">
        <item name="android:divider">@null</item>
        <item name="android:listSelector">@drawable/overflow_selector</item>


    </style>

</resources>

现在在代码中包含这个xml:

setTheme(Theme.Styled);

现在更改这三行,当您更改这三行时,图标将会改变:

  <item name="android:icon">@drawable/ic_nav_home_back</item>
    <item name="android:homeAsUpIndicator">@drawable/ic_nav_back</item>
    <item name="icon">@drawable/ic_nav_home_back</item>

答案 3 :(得分:0)

if(getSupportActionBar() != null) {
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.drawable.add_contact);
    }