自定义应用标题

时间:2013-03-20 14:17:56

标签: android android-layout android-widget

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

<?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>

1 个答案:

答案 0 :(得分: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">