简体   繁体   English

如何根据用户输入更改android应用的背景颜色?

[英]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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改我的 Android 应用程序列的背景颜色 - How do I change the background color for a column of my Android app 如何更改 Android 应用程序中时间选择器的颜色? - How do I change the color of the timepicker in my Android app? 如何更改我的按钮颜色,使用 android:background 给它白色边框? - How do I change my Buttons color, using android:background gives it white borders? 如何在 Android 中更改 canvas 上的油漆颜色/厚度? - How do I change the color/thickness of my paint on a canvas in Android? 如何更改背景颜色? - How do I make the background color change? 如何在不更改Android中操作栏颜色的情况下更改应用程序背景颜色 - How can I change the app background color without changing the action bar color in android 如何根据给定的数据作为android中的参数更改我的listview的背景颜色 - how to change the background color of my listview based on a given data as a parameter in android 如何在Android应用程序的警报对话框中为用户输入编码? - How do I code for user input in an alert dialog for an android app? 如何使用彩色按钮作为用户选择从第二个活动更改我的 MainActivity 的背景颜色? - How can I change the background color of my MainActivity from a second activity using colored buttons as user choices? 如何根据文本所在的背景更改文本的前景色? - How can I change the foreground color of a text based on the background it is on?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM