2017-10-02 3 views
0

私はカスタムラジオボタンを作りたいと思います。通常(radio_normal)状態のドロアブルを作成しました。しかし、チェック状態(radio_pressed)のために形状を作成するのは難しいです。しかし、うまくいきませんでした。正しい方法で行ったかどうかは分かりません。チェック状態の場合は、このように見えるはずです。外枠と内円は、白でなければなりません。それ?Androidカスタムラジオボタン

radio_normal:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="ring" 
    android:useLevel="false"> 
    <solid 
     android:color="#2196f3"></solid> 

</shape> 

はradio_pressed:

<?xml version="1.0" encoding="utf-8"?> 
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
    <shape android:shape="oval" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 
     <solid 
      android:color="@android:color/transparent"> 
     </solid> 
     <size 
      android:height="60dp" 
      android:width="60dp"> 
     </size> 
     <stroke 
      android:color="#ffffff" 
      android:width="1dp" 
      > 
    </stroke> 
    </shape> 
     </item> 
     <item> 
      <shape android:shape="oval"> 
       <solid 
        android:color="#ffffff"> 
       </solid> 
      </shape> 
      <size 
       android:height="30dp" 
       android:width="30dp"> 
    </size> 
     </item> 
    </layer-list> 

radio_bg:

<?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/radio_pressed" 
    > 
</item> 
    <item 
     android:state_checked="false" 
     android:drawable="@drawable/radio_normal" 
     > 
    </item> 
</selector> 

activity_main:

<RadioButton 
     android:id="@+id/radio" 
     android:layout_centerInParent="true" 
     android:layout_width="10dp" 
     android:button="@drawable/radio_bg" 
     android:layout_marginTop="100dp" 
     android:layout_height="10dp" 
     /> 

答えて

1

のサイズ、それはあなたの側で動作し、それが役立ちます:)

radio_normalことを願っています:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="ring" 
    android:useLevel="false"> 
    <solid android:color="#2196f3" /> 
    <size 
     android:width="25dp" 
     android:height="25dp" /> 
</shape> 

はradio_pressed:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:padding="3dp"> 
    <item> 
     <shape xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="ring" 
      android:useLevel="false"> 
      <solid android:color="@android:color/white" /> 
      <size 
       android:width="25dp" 
       android:height="25dp" /> 
     </shape> 
    </item> 
    <item 
     android:bottom="7dp" 
     android:left="7dp" 
     android:right="7dp" 
     android:top="7dp"> 
     <shape 
      android:shape="oval" 
      android:useLevel="false"> 

      <solid android:color="@android:color/white" /> 
     </shape> 
    </item> 
</layer-list> 

radio_bg:

<?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/radio_pressed" 
     > 
    </item> 
    <item 
     android:state_checked="false" 
     android:drawable="@drawable/radio_normal" 
     > 
    </item> 
</selector> 

activity_main:

<RadioButton 
    android:id="@+id/radio" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="100dp" 
    android:button="@drawable/radio_bg"/> 

これがあなたのために働いていた場合、私に教えてください:)

+0

それは働いた...ありがとうございました....あなたの時間をありがとう。 – jobin

+0

しかし、問題は、match_parentまたはwrap_contentの場合にのみ、ラジオボタンの特定の寸法を設定できないことです。寸法を指定すると、不規則な形状になります。 – jobin

0

これはあなたが探していたかもしれませんか?

はradio_pressed:コメントする

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="ring" 
      android:useLevel="false"> 
      <solid android:color="@android:color/white" /> 
      <size 
       android:width="10dp" 
       android:height="10dp" /> 
     </shape> 
    </item> 
    <item 
     android:bottom="3dp" 
     android:left="3dp" 
     android:right="3dp" 
     android:top="3dp"> 
     <shape 
      android:shape="oval" 
      android:useLevel="false"> 

      <solid android:color="@android:color/white" /> 
     </shape> 
    </item> 
</layer-list> 

反応:

You should get this when you change bottom, top, left and right to 50 dp

をコード:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="ring" 
      android:useLevel="false"> 
      <solid android:color="@android:color/white" /> 
      <size 
       android:width="150dp" 
       android:height="150dp" /> 
     </shape> 
    </item> 
    <item 
     android:bottom="50dp" 
     android:left="50dp" 
     android:right="50dp" 
     android:top="50dp"> 
     <shape 
      android:shape="oval" 
      android:useLevel="false"> 

      <solid android:color="@android:color/white" /> 
     </shape> 
    </item> 
</layer-list> 

注:のみCH 150dp X 150dpのあなたのイメージビューに対応するための大きさになっていました。あなたのラジオボタンのためにそれを使用している場合、私は非常に10x10dpを使用してreccomendこれは私があなたを得ることができると同じくらい近い

+0

私はそれが表示されている場合、私は寸法150DP型でのみ白い円形の背景を見ることができる見ることがImageViewの背景として設定 – jobin

+0

なお アンドロイド:ボトム= "3DP" アンドロイド:右= "3DP" アンドロイド:上部= "3DP が習慣図でリサイズ= "3DP" アンドロイドを残しました。これらの値を50または60dpに変更します(寸法は150x150dpなので)。あなたはそれを見なければなりません。私が提供するコードを10x10dpで使用すると、白いチェックラジオボタンが表示されます。 –

+0

あなたが言ったように、上端と他の寸法を50dpに変更しました。ラジオボタンが表示されません – jobin

関連する問題