2011-01-21 185 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")); 
     } 
    }); 
} 
相关问题