CheckBox
Androidでデフォルトの色を変更するにはどうすればよいですか?
デフォルトのCheckBox
色は緑ですが、この色を変更したいと思います。
できない場合はカスタムの仕方を教えてくださいCheckBox
。
25 に答える
XML で直接色を変更できます。buttonTint
ボックスに使用: (API レベル 23 以降)
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/CHECK_COLOR" />
appCompatCheckbox v7
古い API レベルを使用してこれを行うこともできます。
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/COLOR_HERE" />
21 歳以上の場合minSdkVersion
は、属性を使用android:buttonTint
してチェックボックスの色を更新します。
<CheckBox
...
android:buttonTint="@color/tint_color" />
AppCompat ライブラリを使用し、21 未満の Android バージョンをサポートするプロジェクトでは、互換バージョンのbuttonTint
属性を使用できます。
<CheckBox
...
app:buttonTint="@color/tint_color" />
この場合、 a をサブクラス化したい場合は、代わりCheckBox
に使用することを忘れないでください。AppCompatCheckBox
前の回答:
属性CheckBox
を使用してドローアブルを変更できます。android:button="@drawable/your_check_drawable"
チェックボックスのAndroidテーマを設定して、styles.xmlに必要な色を追加できます:
<style name="checkBoxStyle" parent="Base.Theme.AppCompat">
<item name="colorAccent">CHECKEDHIGHLIGHTCOLOR</item>
<item name="android:textColorSecondary">UNCHECKEDCOLOR</item>
</style>
次に、レイアウトファイルで:
<CheckBox
android:theme="@style/checkBoxStyle"
android:id="@+id/chooseItemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
このメソッドを使用するのとは異なりandroid:buttonTint="@color/CHECK_COLOR"
、Api 23 で動作します
buttonTint
21 以上の API バージョンのボタンとカラー セレクターの色を変更 するために使用します。
<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/checkbox_filter_tint"
tools:targetApi="21"/>
res/colors/checkbox_filter_tint.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/light_gray_checkbox"
android:state_checked="false"/>
<item android:color="@color/common_red"
android:state_checked="true"/>
</selector>
プログラム版:
int states[][] = {{android.R.attr.state_checked}, {}};
int colors[] = {color_for_state_checked, color_for_state_normal}
CompoundButtonCompat.setButtonTintList(checkbox, new ColorStateList(states, colors));
あなたのxmlにbuttonTintを追加してください
<CheckBox
android:id="@+id/chk_remember_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@android:color/white"
android:text="@string/hint_chk_remember_me" />
styles.xml
ファイルに次の行を追加します。
<style>
<item name="android:colorAccent">@android:color/holo_green_dark</item>
</style>
私は同じ問題に直面しました。以下のテクニックを使用して解決策を得ました。をプロジェクトのドローアブル フォルダーにコピーしbtn_check.xml
、android-sdk/platforms/android-#(version)/data/res/drawable
「オン」および「オフ」の画像状態をカスタム画像に変更します。
次に、xmlが必要になりますandroid:button="@drawable/btn_check"
<CheckBox
android:button="@drawable/btn_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
別のデフォルトの Android アイコンを使用する場合は、次を使用できます。
android:button="@android:drawable/..."
xmlDrawable resource file
を作成しres->drawable
、名前を付けます。たとえば、checkbox_custom_01.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="@drawable/checkbox_custom_01_checked_white_green_32" />
<item
android:state_checked="false"
android:drawable="@drawable/checkbox_custom_01_unchecked_gray_32" />
</selector>
カスタム チェックボックスの画像ファイル (png をお勧めします) をres->drawable
フォルダーにアップロードします。
次に、レイアウトファイルに移動し、チェックボックスを次のように変更します
<CheckBox
android:id="@+id/checkBox1"
android:button="@drawable/checkbox_custom_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="CheckBox"
android:textSize="32dip"/>
android:button
以前に作成した正しい XML ファイルを指している限り、何でもカスタマイズできます。
初心者への注意: 必須ではありませんが、レイアウト ツリー全体で一意の ID をチェックボックスに付けることをお勧めします。
100% 堅牢なアプローチ。
私の場合、サード パーティの MaterialDialog ライブラリから Checkbox を取得したため、XML レイアウト ソース ファイルにアクセスできませんでした。したがって、これをプログラムで解決する必要があります。
- xml で ColorStateList を作成します。
res/color/ checkbox_tinit_dark_theme.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white"
android:state_checked="false"/>
<item android:color="@color/positiveButtonBg"
android:state_checked="true"/>
</selector>
次に、チェックボックスに適用します。
ColorStateList darkStateList = ContextCompat.getColorStateList(getContext(), R.color.checkbox_tint_dark_theme); CompoundButtonCompat.setButtonTintList(checkbox, darkStateList);
PSさらに、誰かが興味を持っている場合は、MaterialDialogダイアログからチェックボックスを取得する方法を次に示します(で設定した場合.checkBoxPromptRes(...)
):
CheckBox checkbox = (CheckBox) dialog.getView().findViewById(R.id.md_promptCheckbox);
お役に立てれば。
ほとんどの回答は、xml ファイルを通過します。ほとんどの Android バージョンでアクティブな回答が見つかり、2 つのステータスに対して 1 つの色しかない場合 Check と UnCheck: これが私の解決策です:
コトリン:
val colorFilter = PorterDuffColorFilter(Color.CYAN, PorterDuff.Mode.SRC_ATOP)
CompoundButtonCompat.getButtonDrawable(checkBox)?.colorFilter = colorFilter
ジャワ:
ColorFilter colorFilter = new PorterDuffColorFilter(Color.CYAN, PorterDuff.Mode.SRC_ATOP);
Drawable drawable = CompoundButtonCompat.getButtonDrawable(checkBox);
if (drawable != null) {
drawable.setColorFilter(colorFilter);
}
drawable で独自の xml を作成し、これを android:background="@drawable/your_xml" として使用できます。
ボーダーコーナーにすべてを与えることができるという点で
<item>
<shape>
<gradient
android:endColor="#fff"
android:startColor="#fff"/>
<corners
android:radius="2dp"/>
<stroke
android:width="15dp"
android:color="#0013669e"/>
</shape>
</item>
以下のコードを試してください。それは私のために働いています。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checked"
android:state_checked="true">
<color android:color="@color/yello" />
</item>
<!-- checked -->
<item android:drawable="@drawable/unchecked"
android:state_checked="false">
<color android:color="@color/black"></color>
</item>
<!-- unchecked -->
<item android:drawable="@drawable/checked"
android:state_focused="true">
<color android:color="@color/yello"></color>
</item>
<!-- on focus -->
<item android:drawable="@drawable/unchecked">
<color android:color="@color/black"></color>
</item>
<!-- default -->
</selector>
とチェックボックス
<CheckBox
Button="@style/currentcy_check_box_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="@string/step_one_currency_aud" />
を設定することにより、プログラムで色を設定するはるかに簡単な方法がありますColorStateList
。
Checkbox.setButtonTintList(ColorStateList.valueOf(getContext().getColor(R.color.yourcolor)))
<CheckBox>
の中に埋め込むことで、の背景色を変更できます<LinearLayout>
。次に、の背景色<LinearLayout>
を希望の色に変更します。
上記のように、Androidアイコンを使用する場合..
android:button="@android:drawable/..."
..これは良いオプションですが、これを機能させるには、次のように、チェックマークを表示/非表示にするトグルロジックを追加する必要があることがわかりました。
checkBoxShowPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// checkbox status is changed from uncheck to checked.
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int btnDrawable = android.R.drawable.checkbox_off_background;
if (isChecked)
{
btnDrawable = android.R.drawable.checkbox_on_background;
}
checkBoxShowPwd.setButtonDrawable(btnDrawable);
}
});
私のminSdkVersionは15
、私のBaseAppThemeの親はTheme.AppCompat.Light.NoActionBar
、プログラムでチェックボックスを作成しています。次の手順は私にとってはうまくいきました。
1. Javaコードで、以下を変更します
CheckBox checkBox = new CheckBox(context);
に
AppCompatCheckBox checkBox = new AppCompatCheckBox(context);
2. styles.xmlに以下を追加します。
<style name="MyCheckboxStyle" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="buttonTint">@color/primary_dark</item>
</style>
3.最後に、BaseAppTheme (またはAppTheme ) スタイル内に以下を追加します。
<item name="checkboxStyle">@style/MyCheckboxStyle</item>
<item name="android:checkboxStyle">@style/MyCheckboxStyle</item>