更改Android中布局的背景颜色

qwe*_*rty 12 android

我有一个简单的Android应用程序,有3个按钮.当我点击第一个按钮时,我想要改变布局的背景颜色(现在是白色......当我按下按钮时,我想改变其他颜色).我怎样才能做到这一点?

在那个按钮上,我有一个myClickHndler事件

    public void myClickHandler(View view) {
    switch (view.getId()) {
    case R.id.Button01:
        text.setText("Button 1 was clicked");
        break;
    case R.id.Button03:
        //text.setText("Button 3 was clicked");
                    .................... // ?
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

Sam*_*muh 25

给你LinearLayout这样的ID :

<LinearLayout android:id="@+id/laidout"
        ...>
Run Code Online (Sandbox Code Playgroud)

然后从你的java类说:

...
case R.id.Button03:
//text.setText("Button 3 was clicked");
  .................... // ?
mlayout= findViewById(R.id.laidout);
// set the color 
mlayout.setBackgroundColor(Color.WHATEVER);
// you can use setBackgroundResource() and pass appropriate ID
// if you want a drawable bundled as resource in the background
mlayout.setBackgroundResource(R.drawable.background_img);
break;
...
Run Code Online (Sandbox Code Playgroud)

[编辑]:为评论中要求的内容添加了代码