如何为 android 中的应用程序标题字符串着色

how to give color to the application title string in android

我想在我的应用程序中为应用程序标题字符串提供 rgb 颜色 我这样声明的资源文件中的字符串,

<string name="app_name">MyApplication</string>

并用它来设置清单文件中的标题,

<activity
        android:name=".MainActivity"
        android:screenOrientation="portrait" 
        android:theme="@style/MyTheme"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"> 

我想给 MyApplication 单词不同的颜色, rgb(201,89,51) 分别代表 My 和 rgb(74,137,174)

我该怎么做?

试试这个

 getActionBar().setTitle(Html.fromHtml("<font color='#ff0000'>My</font><font color='#ff00ff'>Application</font>"));

如果您使用的是 appcompact :

getSupportActionBar().setTitle(Html.fromHtml("<font color='#ff0000'>My</font><font color='#ff00ff'>Application</font>"));

你试过了吗Html.fromHtml(String s)?输出为您提供 HTML 文本样式。一个例子:

String styledText=Html.fromHtml("<font color="#123456">My</font> <font color="#654321">App</font>");

然后设置:

Window w = getWindow();
w.setTitle(styledText);

getSupportActionBar().setTitle(styledText);
//getActionBar().setTitle(styledText);