gpt4 book ai didi

android - 在 Android 中更改布局的背景颜色

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:33:32 25 4
gpt4 key购买 nike

我有一个简单的 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;
}
}

谢谢!

最佳答案

像这样给你的 LinearLayout 一个 Id:

<LinearLayout android:id="@+id/laidout"
...>

然后从你的 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;
...

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

关于android - 在 Android 中更改布局的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2895367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com