2013-08-21 23 views
5

muszę utworzyć 2 kształty dzwonienia do moich przycisków radiowych:Android kształt pierścienia na przycisk radiowy

  1. białym kole
  2. białe koło z innego kręgu wewnątrz niego z innym kolorem

I Nie mam pojęcia, jak to zrobić. Co próbowałem dotąd:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_checked="false"><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring"> 
      <android:solid android:color="@color/white" /> 

      <android:size android:height="10dp" android:width="10dp" /> 

      <corners android:radius="10dp" /> 
     </shape></item> 

</selector> 

<RadioButton 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:button="@drawable/radio_shape_unchecked" 
         android:checked="false" 
         android:text="Persoana fizica" /> 

http://i.stack.imgur.com/mltby.png

Odpowiedz

22

Oto kod you..You może zrobić coś takiego. Jeśli masz jakiś problem, to mogę wysłać ci cały projekt .. Mam nadzieję, że to pomoże Tobie i innym. !!

Res/odkształcalne/red_ring.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:innerRadiusRatio="3" 
    android:shape="ring" 
    android:thickness="10dp" 
    android:useLevel="false" > 

    <solid android:color="#FF0000" /> 

    <size 
    android:height="30dp" 
    android:width="30dp" /> 

</shape> 

Res/rozciągliwej/blue_ring.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:innerRadiusRatio="3" 
    android:shape="ring" 
    android:thickness="5dp" 
    android:useLevel="false" > 

    <solid android:color="#0000FF" /> 

    <size 
    android:height="20dp" 
    android:width="20dp" /> 

</shape> 

Res/odkształcalne/layer.xml

<?xml version="1.0" encoding="utf-8"?> 
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
     <item android:drawable="@drawable/red_ring"/> 
     <item android:drawable="@drawable/blue_ring"/> 

    </layer-list> 

Res/odkształcalne/selector_radio.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/layer"></item> 
    <item android:drawable="@drawable/blue_ring"></item> 
</selector> 

res/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

<RadioGroup 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerInParent="true" 
    android:gravity="center" > 

    <RadioButton 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:button="@drawable/selector_radio" 
     android:paddingLeft="30dp" 
     android:text="Radio 1" /> 

    <RadioButton 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:button="@drawable/selector_radio" 
     android:paddingLeft="30dp" 
     android:text="Radio 2" /> 
    </RadioGroup> 

</RelativeLayout> 

Zrzut ekranu:

OutPut

+0

dziękuję prawie dał mi dokładny wygląd (Co potrzebne jest coś w radiu 2, ale zamienione kolory) Spróbuję go edytować i uzyskać wygląd, którego potrzebuję. –

+0

Fajne .. Jeśli uznasz to za pomocne, nie zapomnij o przyjęciu i przegłosowaniu. –

+0

Nie zapomnę, ale mam problem: jak ustawić granicę lub jak mam powiedzieć, aby wybrane radio wyglądało jak na zdjęciu (link w pytaniu), teraz mogę tylko wymyślić, jak ustawić go jako pojedynczą kolor –

0

Rozwiązanie Ketan Ahir, nie działa dla mnie beacouse muszę wypełnione tło. Wreszcie wykonany 1 pierścień kształt i owalny kształt i ustawić rozmiar podczas wyświetlania.

kształt pierścienia

<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="ring" android:useLevel="false"> 
    <solid 
     android:color="#fbad38" /> 
    <stroke 
     android:width="2dp" 
     android:color="#fbad38" /> 
    </shape> 

owalny kształt

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="oval" android:useLevel="false"> 
    <solid android:color="#fbad38"></solid> 
</shape> 

Zmiana rozmiaru Code

frbtRadio1 = (RadioButton) view.findViewById(R.id.radio1); 

    frbtRadio1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      ViewGroup.LayoutParams lp = frbtRadio1.getLayoutParams(); 
      lp.height = frbtRadio1.getWidth(); 

      frbtRadio1.setLayoutParams(lp) 
      frbtRadio1.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
     } 
    }); 
Powiązane problemy