简体   繁体   English

您可以在 Android Studio 中更改布局资源的背景颜色吗?

[英]Can you change backgroundcolor of an layout resource in Android Studio?

I have an activity with a button "Select color" and a edittxt (insert hex code for color).我有一个带有“选择颜色”按钮和一个 edittxt(插入颜色的十六进制代码)的活动。 When user hit the button the backgroundcolor of the resourcefile must change to hex code.当用户点击按钮时,资源文件的背景颜色必须更改为十六进制代码。 The color is now set on XML file.颜色现在设置在 XML 文件上。 How can I change this XML file with user input?如何使用用户输入更改此 XML 文件?

package com.gade.tabletcolor;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class PickColor extends AppCompatActivity {

    View view;
    EditText addColor;

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

        ImageButton button = findViewById(R.id.imageBtn11);
        Button AddColor = findViewById(R.id.colorBtn);
        addColor = findViewById(R.id.txtColor);
        view = this.getWindow().getDecorView();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextpage();
            }
        });

        AddColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setContentView(R.layout.pickcolor);


            }
        });

    }

    public void nextpage(){
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
    }
}
View someView = findViewById(R.id.screen);
View root = someView.getRootView();
root.setBackgroundColor(getResources().getColor(color.white));

or或者

yourView.setBackgroundColor(Color.parseColor("#ffffff"));

Adding to the previous answer.添加到以前的答案。

AddColor.setOnClickListener(new View.OnClickListener() {
  @Override
     public void onClick(View v) {
               yourView.setBackgroundColor(Color.parseColor(<editText>.parseInputToString()));
            }
        });


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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM