programing

ColorStateList를 프로그래밍 방식으로 만들려면 어떻게 해야 합니까?

sourcejob 2023. 9. 16. 08:57
반응형

ColorStateList를 프로그래밍 방식으로 만들려면 어떻게 해야 합니까?

나는 다음을 만들기 위해 노력하고 있습니다.ColorStateList프로그래밍 방식으로 다음을 사용합니다.

ColorStateList stateList = new ColorStateList(states, colors); 

하지만 두 개의 파라미터가 무엇인지 잘 모르겠습니다.

문서에 따르면 다음과 같습니다.

public ColorStateList (int[][] states, int[] colors) 

API 레벨 1에 추가됨

지정된 매핑을 상태에서 색상으로 반환하는 ColorStateList를 만듭니다.

누가 이것을 만드는 방법을 설명해 주실 수 있나요?

상태에 대한 2차원 배열의 의미는 무엇입니까?

사용 가능한 상태 목록은 http://developer.android.com/reference/android/R.attr.html#state_above_anchor 을 참조하십시오.

비활성화, 초점 없음, 확인되지 않은 상태 등에 대해 색상을 설정하려면 상태를 무효화하기만 하면 됩니다.

int[][] states = new int[][] {
    new int[] { android.R.attr.state_enabled}, // enabled
    new int[] {-android.R.attr.state_enabled}, // disabled
    new int[] {-android.R.attr.state_checked}, // unchecked
    new int[] { android.R.attr.state_pressed}  // pressed
};

int[] colors = new int[] {
    Color.BLACK,
    Color.RED,
    Color.GREEN,
    Color.BLUE
};

ColorStateList myList = new ColorStateList(states, colors);

코틀린:

    val states = arrayOf(
        intArrayOf(android.R.attr.state_enabled), // enabled
        intArrayOf(-android.R.attr.state_enabled), // disabled
        intArrayOf(-android.R.attr.state_checked), // unchecked
        intArrayOf(android.R.attr.state_pressed)  // pressed
    )

    val colors = intArrayOf(
        Color.BLACK,
        Color.RED,
        Color.GREEN,
        Color.BLUE
    )

    val myList = ColorStateList(states, colors)

때로는 이 정도면 충분합니다.

int colorInt = getResources().getColor(R.color.ColorVerificaLunes);
ColorStateList csl = ColorStateList.valueOf(colorInt);

코틀린:

val colorInt: Int = context.getColor(R.color.ColorVerificaLunes)
val csl = ColorStateList.valueOf(colorInt)

첫 번째 차원은 상태 집합의 배열이고, 두 번째 차원은 상태 집합 자체입니다.색상 배열은 각 일치하는 상태 집합의 색상을 나열하므로 색상 배열의 길이는 상태 배열의 첫 번째 차원과 일치해야 합니다(또는 상태가 "사용"되면 충돌합니다).여기 및 예제:

ColorStateList myColorStateList = new ColorStateList(
    new int[][] {
        new int[] {
                android.R.attr.state_pressed
            }, //1
            new int[] {
                android.R.attr.state_focused
            }, //2
            new int[] {
                android.R.attr.state_focused, android.R.attr.state_pressed
            } //3
    },
    new int[] {
        Color.RED, //1
            Color.GREEN, //2
            Color.BLUE //3
    }
);

편집 예제: 다음과 같은 xml 색상 상태 목록:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/white"/>
    <item android:color="@color/black"/>
</selector>

이렇게 보일 것입니다.

ColorStateList myColorStateList = new ColorStateList(
        new int[][]{
                new int[]{android.R.attr.state_pressed},
                new int[]{}
        },
        new int[] {
                context.getResources().getColor(R.color.white),
                context.getResources().getColor(R.color.black)
        }
);

다음은 다음과 같은 예입니다.ColorList프로그래밍 방식으로 Kotlin:

val colorList = ColorStateList(
        arrayOf(
                intArrayOf(-android.R.attr.state_enabled),  // Disabled
                intArrayOf(android.R.attr.state_enabled)    // Enabled
        ),
        intArrayOf(
                Color.BLACK,     // The color for the Disabled state
                Color.RED        // The color for the Enabled state
        )
)

유감스럽게도 어떤 해결책도 제게 맞지 않습니다.

  1. 처음에 누름 상태를 설정하지 않으면 감지할 수 없습니다.
  2. 설정한 경우 기본 색상을 추가하려면 빈 상태를 정의해야 합니다.
ColorStateList themeColorStateList = new ColorStateList(
        new int[][]{
                new int[]{android.R.attr.state_pressed},
                new int[]{android.R.attr.state_enabled},
                new int[]{android.R.attr.state_focused, android.R.attr.state_pressed},
                new int[]{-android.R.attr.state_enabled},
                new int[]{} // this should be empty to make default color as we want
        },
        new int[]{
                pressedFontColor,
                defaultFontColor,
                pressedFontColor,
                disabledFontColor,
                defaultFontColor
        }
);

소스 코드의 생성자입니다.

/**
 * Creates a ColorStateList that returns the specified mapping from
 * states to colors.
 */
public ColorStateList(int[][] states, int[] colors) {
    mStateSpecs = states;
    mColors = colors;

    if (states.length > 0) {
        mDefaultColor = colors[0];

        for (int i = 0; i < states.length; i++) {
            if (states[i].length == 0) {
                mDefaultColor = colors[i];
            }
        }
    }
}

Jonathan Ellis의 답변에서 벗어나, Kotlin에서는 도우미 기능을 정의하여 코드를 좀 더 관용적이고 읽기 쉽게 만들 수 있으므로 대신 다음과 같이 쓸 수 있습니다.

val colorList = colorStateListOf(
    intArrayOf(-android.R.attr.state_enabled) to Color.BLACK,
    intArrayOf(android.R.attr.state_enabled) to Color.RED,
)

colorStateListOf다음과 같이 구현할 수 있습니다.

fun colorStateListOf(vararg mapping: Pair<IntArray, Int>): ColorStateList {
    val (states, colors) = mapping.unzip()
    return ColorStateList(states.toTypedArray(), colors.toIntArray())
}

또한 다음을 가지고 있습니다.

fun colorStateListOf(@ColorInt color: Int): ColorStateList {
    return ColorStateList.valueOf(color)
}

선택기든 단색이든 동일한 기능명을 부를 수 있도록 말입니다.

작성을 위한 나의 작성자 클래스ColorStateList

private class ColorStateListBuilder {
    List<Integer> colors = new ArrayList<>();
    List<int[]> states = new ArrayList<>();

    public ColorStateListBuilder addState(int[] state, int color) {
        states.add(state);
        colors.add(color);
        return this;
    }

    public ColorStateList build() {
        return new ColorStateList(convertToTwoDimensionalIntArray(states),
                convertToIntArray(colors));
    }

    private int[][] convertToTwoDimensionalIntArray(List<int[]> integers) {
        int[][] result = new int[integers.size()][1];
        Iterator<int[]> iterator = integers.iterator();
        for (int i = 0; iterator.hasNext(); i++) {
            result[i] = iterator.next();
        }
        return result;
    }

    private int[] convertToIntArray(List<Integer> integers) {
        int[] result = new int[integers.size()];
        Iterator<Integer> iterator = integers.iterator();
        for (int i = 0; iterator.hasNext(); i++) {
            result[i] = iterator.next();
        }
        return result;
    }
}

예제 사용하기

ColorStateListBuilder builder = new ColorStateListBuilder();
builder.addState(new int[] { android.R.attr.state_pressed }, ContextCompat.getColor(this, colorRes))
       .addState(new int[] { android.R.attr.state_selected }, Color.GREEN)
       .addState(..., some color);

if(// some condition){
      builder.addState(..., some color);
}
builder.addState(new int[] {}, colorNormal); // must add default state at last of all state

ColorStateList stateList = builder.build(); // ColorStateList created here

// textView.setTextColor(stateList);

Colors.xml 리소스를 사용하는 경우

int[] colors = new int[] {
                getResources().getColor(R.color.ColorVerificaLunes),
                getResources().getColor(R.color.ColorVerificaMartes),
                getResources().getColor(R.color.ColorVerificaMiercoles),
                getResources().getColor(R.color.ColorVerificaJueves),
                getResources().getColor(R.color.ColorVerificaViernes)

        };

ColorStateList csl = new ColorStateList(new int[][]{new int[0]}, new int[]{colors[0]}); 

    example.setBackgroundTintList(csl);

체크 및 체크 해제를 위해 여기에 있습니다.

   int[][] states = new int[][] {
            new int[] {android.R.attr.state_checked}, //checked  
            new int[] { -android.R.attr.state_checked} // unchecked

    };
    int[] colors = new int[] {
            Color.RED,
            Color.GREEN
    };

    ColorStateList stateList = new ColorStateList(states, colors);

언급URL : https://stackoverflow.com/questions/15543186/how-do-i-create-colorstatelist-programmatically

반응형