Android中动态批改屏幕的背景色

Android中动态修改屏幕的背景色

代码中通过findViewById()获得Layout可以设置Orientation,但设置背景色无效

 

通过如下代码也可以修改背景色:

 

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

LinearLayout linearLay = (LinearLayout) layoutInflater.inflate(R.layout.togglebutton, null) ;

linearLay.setBackgroundColor(Color.WHITE);

 

setContentView(linearLay);

 

但来回切换仍然不行.

 

不通过布局对象,直接修改window的背景属性

 

主要的代码如下:

 

ToggleButton tb=(ToggleButton)findViewById(R.id.toggle);
        tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                 Resources res = getResources();

                if(isChecked){
                    Drawable drawable = res.getDrawable(R.drawable.bgwhite);
                    ToggleActivity.this.getWindow().setBackgroundDrawable(drawable);
                }
                else{
                    Drawable drawable = res.getDrawable(R.drawable.bgblack);
                    ToggleActivity.this.getWindow().setBackgroundDrawable(drawable);
                }
              
        });