【问题标题】:Title bar color change issues标题栏颜色变化问题
【发布时间】:2012-06-22 04:48:36
【问题描述】:

我想动态改变标题栏的颜色,即:有人点击一个按钮,它改变了颜色。但是,我似乎无法让它填满整个标题栏。这发生在模拟器和 Nexus One 上。有什么想法吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/title_bar" android:background="@color/title_bar_blue" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:id="@+id/title_left_text" android:layout_alignParentLeft="true" style="?android:attr/windowTitleStyle" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    <TextView android:id="@+id/title_right_text" android:layout_alignParentRight="true" style="?android:attr/windowTitleStyle" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</RelativeLayout>

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.main);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

    titleBar = (RelativeLayout) findViewById(R.id.title_bar);
    titleBar.setBackgroundResource(R.color.title_bar_green);

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这个((View) titleBar.getParent()).setBackgroundColor(R.color.title_bar_green); 这不是完成这项工作的最佳方式。但是如果它有效,那么您将知道您需要设置R.id.title_bar parent 的背景颜色。

    【讨论】:

    • 谢谢,确实有效。不过很好奇,如果这不是完成这项工作的最佳方式,那是什么?
    • 打电话给getParent()不是个好习惯,有时甚至会很危险。
    • 您可以尝试使用findViewById(titleContainerId) 使用以下代码获取title_bar 的父级:// retrieve value for com.android.internal.R.id.title_container(=0x1020149) int titleContainerId = (Integer) Class.forName("com.android.internal.R$id").getField("title_container").get(null);
    【解决方案2】:

    试试这个

    titleBar = (RelativeLayout) findViewById(R.id.title_bar);
    final FrameLayout titleContainer = (FrameLayout)titleBar.getParent(); 
     titleContainer.setPadding(0, 0, 0, 0)
    titleBar.setBackgroundResource(R.color.title_bar_green);
    

    【讨论】:

    • 如果给title_bar的父级设置padding 0,文本会移动到左边吗?
    猜你喜欢
    • 1970-01-01
    • 2019-03-01
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 2016-08-31
    • 2017-03-06
    相关资源
    最近更新 更多