【问题标题】:Customise app title自定义应用标题
【发布时间】:2013-03-20 14:17:56
【问题描述】:

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

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

【问题讨论】:

  • 是的。不要仅仅因为您想要不同的文本颜色而设置自定义视图。借助自定义主题进行更改。
  • 是否可以使用主题仅更改应用标题颜色?

标签: android android-layout android-widget


【解决方案1】:

您可以使用自定义主题轻松做到这一点。这样做的过程是这样的

在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">

【讨论】:

  • 那么这会在整个应用程序中应用吗?我已经有了该应用程序的主题。现在我需要为应用程序标题设置与自定义主题中提到的不同的文本颜色。
  • 是的,它成功了!但我无法得出它这样做的原因。 android:windowTitleBackgroundStyle 从来都不是标准参数。那它怎么就完全改变了标题文本的颜色呢?
  • 它只是设置标题栏样式,你可以看到它继承了 android:Theme.. 其他属性可以在第一个样式中设置
  • 查看我编辑的问题!我明确地将文本颜色设置为黑色。除此之外,自定义样式位将文本颜色设置回白色。但是,实际上,除了应用程序标题之外的所有文本都变成了黑色。我无法理解这一点
  • 因为在第二种样式中,您将颜色设置为黑色
猜你喜欢
  • 2022-01-22
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 2014-01-09
  • 2013-09-02
  • 2015-05-07
相关资源
最近更新 更多