[英]How do I change the background color of my android app based on user input?
I have three difficulty modes for my app; 我的应用程序有三种难度模式; Easy, Medium, and Hard.
容易,中等和困难。 I have a variable that keeps track of the difficulty (0-2).
我有一个跟踪难度(0-2)的变量。 How can I use this variable to change the background color of my app?
如何使用此变量更改应用程序的背景颜色? For example, when the mode is easy I want the background to be green, yellow for medium, and red for hard.
例如,当模式很容易时,我希望背景为绿色,黄色为中等,红色为硬。
Here is what I have in my layout. 这就是我的布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/myBackground" >
</RelativeLayout>
And I have the string resource: 而且我有字符串资源:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="myBackground">#01CC18</color>
</resources>
However, what I want to do is be able to change the background in the program itself instead of in the xml layouts. 但是,我想做的是能够在程序本身而不是xml布局中更改背景。 Can this be done?
能做到吗?
Fill your main view with a LinearLayout. 用LinearLayout填充主视图。 Then get a handle to this layout:
然后获取此布局的句柄:
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
You can use this handle to update the background color whenever your difficulty changes: 每当您的难度发生变化时,都可以使用此手柄来更新背景颜色:
switch(difficulty) {
case 0:
layout.setBackgroundColor(android.R.color.green);
break;
case 1:
layout.setBackgroundColor(android.R.color.orange);
break;
case 2:
layout.setBackgroundColor(android.R.color.red);
break;
default:
break;
}
I think this is what are you looking for: 我认为这是您要寻找的:
http://android-developers.blogspot.com/2009/03/window-backgrounds-ui-speed.html http://android-developers.blogspot.com/2009/03/window-backgrounds-ui-speed.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.