2012-07-13 19 views
42

私はいくつかの場所を検索しましたが、チェックボックスの枠線にはCheckBoxのドロアブルがありません。誰かが私を正しい方向に向けることができますか?ここでAndroid:CheckBoxの色を設定してください

はここ

enter image description here

(かろうじてボックスを見ることができます)、それは未チェックのように見えるものですチェック状態

enter image description here

ここでは、私が作るしようとしているものですですように見えます。

enter image description here

+0

あなたはcheckBox.soの背景として画像を設定することができますが有する画像を撮ります国境とそれの背景を設定します。 – AkashG

+3

'android:buttonTint =" @ color/mybrown "を設定すると、チェックボックスの色を簡単に変更できます。 – shauvik

+0

android:buttonTint = "@ color/mybrown"は、チェックボックスの境界線の色を変更します。唯一の欠点は、それはあなたが望まない可能性のあるティックの色も変えることです。 – jetty

答えて

54

あなたは、このためのカスタムチェックボックスのxmlファイルを使用することができます。それはcustom_checkbox.xmldrawablesフォルダ内の以下の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/cbchk_blue" 
     android:state_focused="false"> 
    </item> 
    <item android:state_checked="true" 
     android:drawable="@drawable/cbchk_blue" 
     android:state_focused="true"> 
    </item> 
    <item android:state_checked="false" 
     android:drawable="@drawable/cbunchk_blue" 
     android:state_focused="false"> 
    </item> 
    <item android:state_checked="false" 
     android:drawable="@drawable/cbunchk_blue" 
     android:state_focused="true"> 
    </item> 
</selector> 

次に、このようなあなたのチェックボックスの背景として、このファイルを使用します。ここで私は私が使用私自身の画像をアップロードしています

 <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:button="@drawable/custom_checkbox" 
     android:id="@+id/checkBox" /> 

cbchk_bluecbunchk_blueの代わり

Unchecked CheckBox Checked CheckBox

+0

それはそれがそれのようにレイアウトされて見て多くの意味があります。良い例を見つけるのに苦労しました。 – Kirk

+0

Ughhhh、なぜチェックボックスに明示的に幅と高さを指定する必要があるのですか?他のビューでは、背景リソース用に(長方形の)ドロウアブルで定義された図形を使用しました。ビューの背景色は、 "属性値であり、"ストローク "はビューの周囲を完全にペイントします。私はチェックボックスで、幅/高さが省略されている場合は、形状が0に崩壊することがわかります!ありがとう。 – samosaris

+1

セレクタをチェックボックスのボタンドロワブルとして追加します。 –

33

Activity HollowとWhite BackgroundのテーマHolo Darkを使用すると同じ問題が発生します。チェックボックスにはダークスタイルがあります。簡単な回避策は、直接Androidのホロ光から背景を設定されています

int id = Resources.getSystem().getIdentifier("btn_check_holo_light", "drawable", "android"); 
checkBox.setButtonDrawable(id); 

あなたはこのすべてのものは、以下の答えでどのように機能するかを偉大な概要を見つけることができます:それは自由にすることが可能であるアンドロイド5とAPIレベル21ので https://stackoverflow.com/a/10139809/1170154

+0

過去数時間私が探していたものとまったく同じです。ありがとう!! – jakeneff

+2

これは私が探していたドロイドです。ありがとう!! – codinguser

+0

これは私にとって完璧に機能しました!ありがとう! – wyzkid207

27

チェックボックスの色(および他の多くのウィジェット)を選択します。あなたはvalues/styles.xmlで、以前のAPIのフォールバックを持っていることを確認をしながら(自分のvalues-v21/styles.xmlに以下を追加します。

<style name="CustomCheckBox"> 
    <item name="android:theme">@style/CheckBoxAppTheme</item> 
</style> 

<style name="CheckBoxAppTheme"> 
    <item name="android:colorAccent"> 
     @color/theFillColorInCheckedState 
    </item> 
    <item name="android:colorControlNormal"> 
     @color/theBorderColorInUncheckedState 
    </item> 
    <item name="android:colorControlHighlight"> 
     @color/theBackgroundColorWhenFocusingTheCheckBox 
    </item> 
</style> 

その後、あなたは自分のレイアウトであなたのチェックボックスにスタイルを適用する必要があります。

<CheckBox 
    style="@style/CustomCheckBox" /> 

だとあなたの好きな色でチェックボックスが表示されます!

+4

あなたは 'android:colorControlActivated'が不足していると思います。 – JJD

+0

@Phoca、以前のレベル21のフォールバックスタイルを作成するにはどうすればよいですか? – lagos

+1

レベル21以前のスタイルでは、 ' lagos

14

大変申し訳ありませんが、これらの回答のほとんどが不完全であるか、マイナーなバグがあります。Androidのさまざまなバージョン間での「スタイリング」コントロールはepicお尻の痛み。デザインの制約が非常に厳しいプロジェクトで私の髪を引き出した後は、テストアプリを書いて、スタイリングスイッチやチェックボックス用のさまざまなソリューションを実際に試してテストしました。それはしばしばもう一方を持っています。ここに私が見つけたのは...

最初に:あなたは実際にそれらのいずれかをスタイルすることはできませんが、それらのすべてにテーマを適用することも、その1つだけを適用することもできます。

第2:あなたはXMLからすべてを行うことができ、2番目のvalues-v21/styles.xmlは必要ありません。

サード:それはあなたがAndroidの(私はあなたが確信しているように)の古いバージョンをサポートしたい場合は、2つの基本的な選択肢があり、スイッチに付属しています...

  1. あなたはSwitchCompatを使用することができますプラットフォーム間で同じように見えるようにすることができます。
  2. あなたはSwitchを使用することができます。テーマの残りの部分、または特定のスイッチだけをテーマにすることができます。古いバージョンのAndroidでは、スタイルのない旧式の四角形のスイッチが表示されます。

ここでは単純な参照コードです。繰り返しますが、単純なHello Worldを作成した場合も同様です。このコードをドロップすると、あなたの心のコンテンツに遊ぶことができます。私はちょうど活動のためのXMLとスタイルを含めるつもりですので、すべてのことは...

activity_main.xml ...

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    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.kunai.switchtest.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="'Styled' SwitchCompat" /> 

    <android.support.v7.widget.SwitchCompat 
     android:id="@+id/switch_item" 
     android:layout_width="wrap_content" 
     android:layout_height="46dp" 
     android:layout_alignParentEnd="true" 
     android:layout_marginEnd="16dp" 
     android:checked="true" 
     android:longClickable="false" 
     android:textOff="OFF" 
     android:textOn="ON" 
     app:switchTextAppearance="@style/BrandedSwitch.text" 
     app:theme="@style/BrandedSwitch.control" 
     app:showText="true" /> 

</RelativeLayout> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    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.kunai.switchtest.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Themed SwitchCompat" /> 

    <android.support.v7.widget.SwitchCompat 
     android:id="@+id/switch_item2" 
     android:layout_width="wrap_content" 
     android:layout_height="46dp" 
     android:layout_alignParentEnd="true" 
     android:layout_marginEnd="16dp" 
     android:checked="true" 
     android:longClickable="false" /> 

</RelativeLayout> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    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.kunai.switchtest.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Themed Switch" /> 

    <Switch 
     android:id="@+id/switch_item3" 
     android:layout_width="wrap_content" 
     android:layout_height="46dp" 
     android:layout_alignParentEnd="true" 
     android:layout_marginEnd="16dp" 
     android:checked="true" 
     android:longClickable="false" 
     android:textOff="OFF" 
     android:textOn="ON"/> 

</RelativeLayout> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    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.kunai.switchtest.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="'Styled' Switch" /> 

    <Switch 
     android:id="@+id/switch_item4" 
     android:layout_width="wrap_content" 
     android:layout_height="46dp" 
     android:layout_alignParentEnd="true" 
     android:layout_marginEnd="16dp" 
     android:checked="true" 
     android:longClickable="false" 
     android:textOff="OFF" 
     android:textOn="ON" 
     android:theme="@style/BrandedSwitch"/> 

</RelativeLayout> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    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.kunai.switchtest.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="'Styled' CheckBox" /> 

    <CheckBox 
     android:id="@+id/checkbox" 
     android:layout_width="wrap_content" 
     android:layout_height="46dp" 
     android:layout_alignParentEnd="true" 
     android:layout_marginEnd="16dp" 
     android:checked="true" 
     android:longClickable="false" 
     android:theme="@style/BrandedCheckBox"/> 

</RelativeLayout> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    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.kunai.switchtest.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Themed CheckBox" /> 

    <CheckBox 
     android:id="@+id/checkbox2" 
     android:layout_width="wrap_content" 
     android:layout_height="46dp" 
     android:layout_alignParentEnd="true" 
     android:layout_marginEnd="16dp" 
     android:checked="true" 
     android:longClickable="false"/> 

</RelativeLayout> 

ここではボイラープレートであります

のstyles.xml ...

<resources> 

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">#3F51B5</item> 
    <item name="colorPrimaryDark">#303F9F</item> 
    <item name="colorAccent">#FF4081</item> 
</style> 

<style name="BrandedSwitch.control" parent="Theme.AppCompat.Light"> 
    <!-- active thumb & track color (30% transparency) --> 
    <item name="colorControlActivated">#e6e600</item> 
    <item name="colorSwitchThumbNormal">#cc0000</item> 
</style> 

<style name="BrandedSwitch.text" parent="Theme.AppCompat.Light"> 
    <item name="android:textColor">#ffa000</item> 
    <item name="android:textSize">9dp</item> 
</style> 

<style name="BrandedCheckBox" parent="AppTheme"> 
    <item name="colorAccent">#aaf000</item> 
    <item name="colorControlNormal">#ff0000</item> 
</style> 

<style name="BrandedSwitch" parent="AppTheme"> 
    <item name="colorAccent">#39ac39</item> 
</style> 

私は知っています、あなたはこれを構築するにはあまりにも怠惰です、あなたはただあなたのコードを書いてみたいです。わかった。

API 21

API_18:

API18

+0

これは完璧な答えです。私の人生を楽にしました。これは受け入れられるものでなければなりません。 – Rachit

1

はこれが最も効率的な方法となります。ここ

API_21 ...それはあなたがそれを実行したときのようになります。

アンドロイド:buttonTint = "黒@カラー/"

+0

tks ...ソシンプル! – cesarsicas

5

あなたはこのAPI21 &

上記のアンドロイドのようなCHECKBOX色を設定することができます。

をbuttonTint = "@カラー/ YOUR_COLOR"
<CheckBox 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:buttonTint="@color/YOUR_COLOR" /> 

古いバージョンのサポート使用AppCompatCheckBox

V7ライブラリのアプリ:buttonTint = "@カラー/ YOUR_COLOR"

<android.support.v7.widget.AppCompatCheckBox 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    app:buttonTint="@color/YOUR_COLOR" /> 
関連する問題