当Android版本支持使用下面发布的代码时,我会更改标题栏。但是,一旦活动实际加载,我似乎无法更改标题栏的颜色。
请参见以下示例:
activity.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.page_title);
LinearLayout ll = ((LinearLayout)activity.getWindow().findViewById(R.id.page_title_bg));
ll.setBackgroundResource(R.drawable.page_title_bg_app_online);
布局非常简单,只包含一个背景布局和一个显示应用程序标题的TextView。
我试过用setBackbroundResrouce设置背景资源,但没能让更改后的标题显示出来。我还尝试在进行更改后使布局无效。
发布于 2011-10-06 19:01:27
只是想一想:你有没有试着设置文本视图的背景(而不是布局)?
很奇怪..。我试过了,对我来说效果很好:
boolean isTitleCustomizible;
try {
isTitleCustomizible = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
} catch (Exception e) {
isTitleCustomizible = false;
}
super.setContentView(resId);
if (isTitleCustomizible) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.title_bar);
View root = getWindow().findViewById(R.id.toolBarRoot);
if (root != null) {
root.setBackgroundResource(android.R.drawable.editbox_background_normal);
}
}
onClick中的代码:
private boolean toggle;
@Override
public void onClick(View v) {
View root = findViewById(R.id.toolBarRoot);
if (root != null) {
if (toggle)
root.setBackgroundResource(0);
else
root.setBackgroundResource(android.R.drawable.editbox_background_normal);
toggle = !toggle;
}
}
https://stackoverflow.com/questions/7679051
复制