动作条自定义标题为ImaveView而不是TextView的自定义、而不是、动作、标题

2023-09-12 02:33:55 作者:Miaow 喵

我试图解决这个在动作条但是从我所看到的标题是basicly一个TextView,但我需要把纯文本的图像来代替。我试着将它设置成风格的themes.xml,如下:

I've trying to work around this on the actionbar but from What I can see the title is basicly a TextView but I need to place an image instead of plain text. I try to set it up into the Style themes.xml as this:

<item name="android:actionBarStyle">@style/MyActionBar</item>

然后,我定义这种风格

And then I defined this style

 <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@drawable/action_background</item>
    <item name="android:titleTextStyle">@style/ActionbarTitleStyle</item>
</style>

我以为我可以定义的背景为标题,但即使我做的不是我想要的,请尝试getActionBar()setCustomView(布局)。但happend我的动作条是空的的setContentView被调用之前,我需要在一个基类。

I thought that I could defined the background for the title but even If I do is not what I want, try the getActionBar().setCustomView(layout); but happend that my actionbar is null before the setContentView is being called and I need it in a Base class.

PD。我只需要关心蜂窝高,所以,如果你愿意不建议福尔摩斯或Jonhanilson实现。

PD. I only need to care about HONEYCOMB and Higher, so if you please not suggest Sherlock or Jonhanilson implementations.

推荐答案

如果你想要一个形象,你可以指定应用程序的标志以及图标(它会被用来代替图标):

if you want an image, you can specify the app logo along with the icon (it will be used instead of icon):

<application
    android:icon="@drawable/ic_launcher"
    android:logo="@drawable/logo"
    android:label="@string/app_name"
    android:name=".AppName"
    >

现在,你有你的lanucher图标的任意图像代替。你下一步需要做什么是摆脱的文字。在您的样式做这样的事情:

Now you have an arbitrary image instead of your lanucher icon. What you need to do next is to get rid of the text. In your styles do something like:

<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.ActionBar">
    <item name="titleTextStyle">@style/NoTitleText</item>
    <item name="android:titleTextStyle">@style/NoTitleText</item>
    <item name="subtitleTextStyle">@style/NoTitleText</item>
    <item name="android:subtitleTextStyle">@style/NoTitleText</item>     
</style>

<style name="NoTitleText">
    <item name="android:textSize">0sp</item>
    <item name="android:textColor">#00000000</item>
</style>

您应该同时使用项目名称=titleTextStyle项目名称=机器人:titleTextStyle如果你使用ActionBarSherlock如果我记得很清楚。

You should use both item name="titleTextStyle" and item name="android:titleTextStyle" if you use ActionBarSherlock if I remember well.