2013-08-21 155 views
5

我需要創建2周環的形狀爲我的單選按鈕:的Android環形狀爲圓形按鈕

  1. 白色圓圈裏面另一個圓用不同的顏色

  • 白色圓沒有太多的線索如何做到這一點。 我試了一下,到目前爲止:

    <?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

  • 回答

    22

    下面是一些代碼you..You可以做這樣的事情。如果你有任何問題,我可以郵寄給你整個項目。希望這可以幫助你和其他人。 !

    RES /抽拉/ 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 /抽拉/ 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 /抽拉/ 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 /抽拉/ 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 /佈局/ 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> 
    

    截圖:

    OutPut

    +0

    非常感謝你幾乎給我確切的樣子(我需要的是像無線電2,但顏色交換)我會嘗試編輯它,並獲得我需要的外觀。 –

    +0

    酷......如果你覺得這個有用,那麼不要忘記接受並注意。 –

    +0

    我不會忘記,但我有一個問題:我該如何設置邊框,或者應該怎麼說,以便選定的收音機看起來像int照片(問題中的鏈接),現在我只能弄清楚如何將它設置爲單個顏色 –

    0

    的科坦阿爾解決方案,而不是我beacouse我需要填補後臺工作。最後製成1環形狀,橢圓形狀,並在顯示時設定大小。

    環形

    <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> 
    

    橢圓形

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

    調整大小代碼

    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); 
         } 
        });