2013-08-21 157 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); 
         } 
        });