2012-06-21 60 views
1

我想動態更改標題欄的顏色,即:某人單擊按鈕,它會更改顏色。但是,我似乎無法讓它填滿整個標題欄。這發生在模擬器和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); 

title bar

回答

1

試試這個((View) titleBar.getParent()).setBackgroundColor(R.color.title_bar_green); 它不是個做最好的方式電子工作。但是,如果它有效,那麼你會知道你需要設置父母的背景顏色R.id.title_bar

+0

謝謝,這確實有用。奇怪的是,如果這不是完成這項工作的最佳方式,那是什麼? – Roger

+0

調用'getParent()'不是一種很好的風格,有時它可能甚至是危險的。 – vasart

+0

你可以試着用'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

0

試試這個

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); 
+0

如果您將填充0設置爲title_bar的父項,文本是否會移動到左邊緣? – vasart

+0

是的,但你可以setPadding到titleBar .............. –