我想動態更改標題欄的顏色,即:某人單擊按鈕,它會更改顏色。但是,我似乎無法讓它填滿整個標題欄。這發生在模擬器和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);
謝謝,這確實有用。奇怪的是,如果這不是完成這項工作的最佳方式,那是什麼? – Roger
調用'getParent()'不是一種很好的風格,有時它可能甚至是危險的。 – vasart
你可以試着用'findViewById(titleContainerId)'得到title_bar的父代:'//獲取com.android.internal.R.id.title_container的值(= 0x1020149) int titleContainerId =(Integer)Class.forName ( 「com.android.internal.R的$ id」)getfield命令( 「title_container」)獲得(空);' – vasart