在我的Android应用程序中,我希望标准/基本标题栏更改颜色。
要更改文本颜色,您有setTitleColor(int color)
,是否可以更改条形的背景颜色?
最佳答案
此thread将帮助您开始在xml文件中构建自己的标题栏并将其用于 Activity 中
编辑
这是上面链接的内容的简要摘要-这是,只是,用于设置文本的颜色和标题栏的背景-无需调整大小,没有按钮,只是最简单的示例
res/layout/mytitle.xml-这是代表标题栏的 View
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myTitle"
android:text="This is my new title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/titletextcolor"
/>
res/values/themes.xml-我们要保留默认的android主题,只需要更改标题背景的背景色即可。因此,我们创建一个继承默认主题的主题,并将背景样式设置为我们自己的样式。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="customTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
</style>
</resources>
res/values/styles.xml-在此处设置主题以使用想要的标题背景颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="WindowTitleBackground">
<item name="android:background">@color/titlebackgroundcolor</item>
</style>
</resources>
res/values/colors.xml-在此处设置所需的颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="titlebackgroundcolor">#3232CD</color>
<color name="titletextcolor">#FFFF00</color>
</resources>
在AndroidMANIFEST.xml中,在应用程序(对于整个应用程序)或 Activity (仅此 Activity )标签中设置主题属性
<activity android:name=".CustomTitleBar" android:theme="@style/customTheme" ...
从 Activity (称为CustomTitleBar):
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
}