2013-03-20 100 views
0

我正在尝试实现自定义应用标题。我做了一个自定义布局,将标题文本颜色更改为白色。自定义应用标题

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="0dp" 
    android:text="@string/title_activity_main" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:textColor="@color/textColorWhite"/> 

而且在活动的onCreate()

requestWindowFeature(Window.FEATURE_LEFT_ICON); 
setContentView(R.layout.activity_home); 
setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.my_icon); 
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.layout_custom_app_title); 

它没有Android 2.2的设备上运行。但是,我在4.0版本上成功测试了

有没有人有任何解决方法?

编辑:

<style name="customWindowTitle"> 
    <item name="android:textColor">@color/textColorWhite</item> 
</style> 

<style name="LightThemeSelector" parent="android:Theme.Light"> 
    <item name="android:windowBackground">@drawable/app_background</item> 
    <item name="android:textColor">@color/textColorBlack</item> 
    <item name="android:windowTitleStyle">@style/customWindowTitle</item> 
</style> 
+0

是。不要仅仅因为您想要不同的文本颜色而设置自定义视图。在自定义主题的帮助下更改它。 – 2013-03-20 14:19:53

+0

是否可以使用主题更改应用标题颜色? – Renjith 2013-03-20 14:21:11

回答

0

您可以通过使用自定义主题很容易做到这一点。要做到这一点这里的程序

在style.xml添加

<resources> 
    <style name="CustomWindowTitleBackground"> 
     <item name="android:background">@color/textColorWhite</item> 
     // here you can add other attributes 
    </style> 

    <style name="CustomTheme" parent="android:Theme"> 
     <item name="android:windowTitleSize">25dip</item> 
     <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> 
    </style> 
</resources> 

,并在AndroidManifest.xml中添加

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/CustomTheme"> 
+0

那么这将被应用到整个应用程序?我已经有了一个应用程序的主题。现在我需要与自定义主题中提到的文字颜色不同的文字颜色来设置应用程序标题。 – Renjith 2013-03-20 14:30:23

+0

是的,它的工作!但我无法理解它的原因。 android:windowTitleBackgroundStyle从来不是一个标准参数。那么如何才能改变标题文字的颜色呢? – Renjith 2013-03-20 14:56:24

+0

它只是设置标题栏的样式,你可以看到它继承了android:Theme ..其他属性可以在第一个样式中设置 – stinepike 2013-03-20 14:59:09