2011-01-21 102 views

回答

119

得到一個處理使用的根佈局,然後設置背景色。根佈局就是你所謂的setContentView。

setContentView(R.layout.main); 

    // Now get a handle to any View contained 
    // within the main layout you are using 
    View someView = findViewById(R.id.randomViewInMainLayout); 

    // Find the root view 
    View root = someView.getRootView(); 

    // Set the color 
    root.setBackgroundColor(getResources().getColor(android.R.color.red)); 
+0

當我這樣做時,Eclipse將其標記爲「應該在這裏通過解析顏色而不是資源ID:getResources()。getColor(android.R.color.red)」。 – joriki 2013-08-06 13:03:25

56
?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#FFFFFF" 
android:id="@+id/myScreen" 
</LinearLayout> 

換句話說,「android:background」是您想要更改的XML中的標記。

如果您需要動態更新的背景值,請參閱以下內容:

Exercise: Change background color, by SeekBar

+1

該問題明確要求如何做到「在Java」... – 2011-01-21 17:34:01

+0

嗯好點。不管如何,我很輕鬆地給出了這個問題的答案。 – I82Much 2011-01-21 18:35:28

+0

我不知道什麼,但它沒有奏效! – SJS 2011-01-21 20:00:19

64

我喜歡着色由主題

<style name="CustomTheme" parent="android:Theme.Light"> 
    <item name="android:windowBackground">@color/custom_theme_color</item> 
    <item name="android:colorBackground">@color/custom_theme_color</item> 
</style> 
196

加入這一行中的活動,之後setContentView()呼叫

getWindow().getDecorView().setBackgroundColor(Color.WHITE); 
7

你可以用它來打電話預定義的android顏色:

element.setBackgroundColor(android.R.color.red); 

如果您想使用自己的自定義顏色之一,則可以將自定義顏色添加到strings.xml,然後使用下面的代碼調用它。

element.setBackgroundColor(R.color.mycolour); 

但是,如果你想在你的layout.xml中設置顏色,你可以修改並添加下面的任何元素來接受它。所以

View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0); 

,改變顏色爲白色:

android:background="#FFFFFF" 
3

要獲得在XML文件中定義的根認爲,如果不採取行動吧,你可以使用這個

root.setBackgroundResource(Color.WHITE); 
3
View randview = new View(getBaseContext()); 
randview = (View)findViewById(R.id.container); 
randview.setBackgroundColor(Color.BLUE); 

爲我工作。謝謝。

1
final View rootView = findViewById(android.R.id.content); 
rootView.setBackgroundResource(...); 
6

在你onCreate()方法:

getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.main_activity_background_color)); 

你也需要添加到文件夾的值稱爲color.xml一個新的XML文件,並分配有一個新的顏色屬性:

color.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<color name="main_activity_background_color">#000000</color> 
</resources> 

請注意,您可以命名爲color.xml任何你想要的名字,但你通過代碼引用它爲R.color.yourId

編輯

因爲getResources().getColor()已過時,使用getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color)); 代替。

1
Button btn; 
View root; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    btn = (Button)findViewById(R.id.button); 

    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      root =findViewById(R.id.activity_main).getRootView(); 
      root.setBackgroundColor(Color.parseColor("#FFFFFF")); 
     } 
    }); 
}