Android:改变背景颜色

时间:2018-03-17 12:04:49

标签: android colors background

我正在尝试编写自己的程序,它允许更改background colour等基本功能。我发现本网站和互联网上的许多条目都是关于菜单颜色而不是主要活动中布局的背景。我想要做的是在this video中完美描述但我想改变菜单中的颜色。

有没有办法实现这个目标?我的代码在这里挂起 - 当在kitkat的虚拟机中运行时没有响应。

public class MainActivity extends AppCompatActivity {

    RadioGroup radioGroup;
    RadioButton radioButtonwhite, radioButton2gray;
    View view;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioButtonwhite = (RadioButton) findViewById(R.id.radioButtonwhite);
        radioButton2gray = (RadioButton) findViewById(R.id.radioButton2gray);

        final View view = findViewById(R.id.screen);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {

                if (checkedId == R.id.radioButton2gray) {
                    view.setBackgroundColor(Color.GRAY);
                }

            }
        });

    }
}

1 个答案:

答案 0 :(得分:0)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.android_examples.com.layoutbackgroundprogrammatically.MainActivity"
android:id="@+id/Layout" >

在您的Activity.class

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 RelativeLayout relativeLayout = (RelativeLayout ) findViewById(R.id.Layout);

 radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
 radioButtonwhite = (RadioButton) findViewById(R.id.radioButtonwhite);
 radioButton2gray = (RadioButton) findViewById(R.id.radioButton2gray);
 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {

        if (checkedId == R.id.radioButton2gray) {
            relativeLayout .setBackgroundResource(R.color.blue);
        }

    }
});

 }

以下是可以找到颜色资源的屏幕截图

color resource

 }