Android can make the color selector change the color of the component with a few simple lines of code, so it can easily realize the skinning function?


I recommend creating a new project or Moudle for testing first.

Preface

To be honest, I didn't understand how to use this framework at first. After downloading and viewing the author's source code, I knew how to use it. Don't talk nonsense, just go to the topic, let's take a look at the effect.
Insert picture description here

Step 1: Import dependencies

If you want to know the source code and how to implement the function, you can go to the source address .
The import dependencies are as follows:

implementation'com.github.kizitonwose.colorpreference:core:1.1.0'

Step 2: The main code

For the convenience of demonstration, the code is simplified, and the code is as follows:

First of all: to inherit the ColorDialog.OnColorSelectedListener method, as shown in the figure: the
Insert picture description here
code is as follows:

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.kizitonwose.colorpreference.ColorDialog;
import com.kizitonwose.colorpreference.ColorShape;

public class MainActivity extends AppCompatActivity implements ColorDialog.OnColorSelectedListener {
    
    
    private Button button;
    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.button);
    }

    public void colorShare(View view){
    
    
        new ColorDialog.Builder(this)
                .setColorShape(ColorShape.SQUARE) //SQUARE 正方形  CIRCLE圆形
                .setColorChoices(R.array.color_choices) //自定义颜色
                .setNumColumns(5)       //设置每行颜色选择的个数
                .setTag("TAG")        //与下方的onColorSelected方法想呼应
                .show();
    }

    @Override
    public void onColorSelected(int newColor, String tag) {
    
    
        if(tag=="TAG"){
    
    
            button.setBackgroundColor(newColor);
        }
    }
}

The main code is as above, and the color selection type is attached below:

<string-array name="color_choices">
        <item>#f44336</item>
        <item>#e91e63</item>
        <item>#9c27b0</item>
        <item>#673ab7</item>
        <item>#3f51b5</item>
        <item>#2196f3</item>
        <item>#03a9f4</item>
        <item>#00bcd4</item>
        <item>#009688</item>
        <item>#4caf50</item>
        <item>#8bc34a</item>
        <item>#cddc39</item>
        <item>#ffeb3b</item>
        <item>#ffc107</item>
        <item>#ff9800</item>
        <item>#ff5722</item>
        <item>#795548</item>
        <item>#9e9e9e</item>
        <item>#607d8b</item>
    </string-array>

Placement:
Insert picture description here
Finally, let’s take a look at the simple effect:
Insert picture description here
if there is anything else you don’t understand, you can look at the source code, download it yourself, and set it to 0 points (CSDN can now set 0 points without automatic growth or it is very user-friendly), the source code address As follows: the
source code address is as follows

Guess you like

Origin blog.csdn.net/qq_45137584/article/details/111294998