2014-02-24 39 views
5

我使用Google提供的支持库中的ActionbarActivity。我为我的操作栏包含按钮和图像附加到右侧进行了自定义视图。问题是一旦活动开始我看到操作栏(应用程序名称)的默认标题和android启动器图标(甚至不是我的启动器图标)出现在左侧一段时间,然后我的自定义视图应用。我该如何解决这个问题。我在没有运气的情况下在onCreateView()之后调用以下代码。即使在我的自定义视图设置后,我也遇到了另一个问题,即它使用Android徽标图标而不是我的启动器图标来保存它的图标。在应用自定义视图之前,操作栏会显示一段时间

this.getSupportActionBar().setDisplayShowCustomEnabled(true); 
this.getSupportActionBar().setDisplayShowTitleEnabled(false); 
getSupportActionBar().setDisplayShowHomeEnabled(false); 
LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

View v = inflator.inflate(R.layout.title_view, null); 
TextView tvTitle = (TextView) v.findViewById(R.id.actionbar_title); 
    this.getSupportActionBar().setCustomView(v); 

编辑1: 图标的问题得到解决。我将支持库作为单独的项目添加。显然,支持库中的启动器图标取代了我的启动器图标。我从支持库项目中删除了所有资源,现在我的图标正常显示。当然,主要问题仍然存在。

+0

我认为你应该改变你的代码的序列,如1)getSupportActionBar()。setDisplayShowHomeEnabled(false); 2)this.getSupportActionBar()setDisplayShowTitleEnabled(假)。 3)this.getSupportActionBar()setDisplayShowCustomEnabled(真)。 –

+0

没有工作。我认为问题不存在,因为这个操作栏行为发生在我的方法甚至调用之前。 – MSaudi

+0

您可以尝试通过使用样式自定义ActionBar风格... –

回答

4

好吧,我做了一些工作,围绕这个

我改变了动作条的我做了有透明图标和文本

<style name="MyTheme" parent="@style/Theme.AppCompat.Light"> 
<item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item> 
</style> 


<style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar"> 
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item> 
    <item name="android:icon">@android:color/transparent</item> 
    <item name="android:background">@drawable/actionbar_bg</item> 
</style> 

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

2-然后在setupActionBar()方法我设置样式回色手动

private void setupActionBar() { 
     getSupportActionBar().setDisplayShowHomeEnabled(false); 
     this.getSupportActionBar().setDisplayShowTitleEnabled(false); 
     this.getSupportActionBar().setDisplayShowCustomEnabled(true); 


    LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = inflator.inflate(R.layout.title_view, null); 
    TextView tvTitle = (TextView) v.findViewById(R.id.actionbar_title); 
     tvTitle.setTextColor(getResources().getColor(R.color.white)); 
.... 
} 
+0

很好的答案。非常感谢。 –