2016-09-15 27 views
1

在我的應用程序中,我想給我的單選按鈕帶有文本的邊框。與Android中的文本RadioButton的邊框

enter image description here

<RadioButton 
    android:id="@+id/radioAndroid" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:button="@drawable/inout_radio_button" 
    android:checked="true" 
    android:gravity="center" 
    android:padding="5dp" 
    android:layoutDirection="rtl" 
    android:layout_marginRight="@dimen/margin_20dp" 
    android:layout_marginLeft="@dimen/margin_20dp" 
    android:text="1" 
    android:textColor="@color/color_radio_text" /> 

<RadioButton 
    android:id="@+id/radioiPhone" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:button="@drawable/inout_radio_button" 
    android:gravity="center" 
    android:padding="5dp" 
    android:layoutDirection="rtl" 
    android:layout_marginRight="@dimen/margin_20dp" 
    android:layout_marginLeft="@dimen/margin_20dp" 
    android:text="2" 
    android:textColor="@color/color_radio_text" /> 

我已經把圖標如下:

inout_radio_button.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="true" android:drawable="@drawable/radioact" ></item> 
    <item android:state_checked="false" android:drawable="@drawable/radio" /> 
</selector> 

但是,我不能找到方法,讓周圍的單選按鈕邊框。

+0

使用父佈局,並給它邊框內,作爲繪製的廣告,您可以設置單選按鈕!!! :) –

回答

4

使用直接將背景參數應用於RadioButton(不需要與外部的佈局圍繞着它):

android:background="@drawable/background"

當背景是這樣的:

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

    <item> 
     <shape 
      android:shape="rectangle"> 
      <stroke 
       android:width="1px" 
       android:color="@android:color/black"/> 
      <corners android:radius="5dp"/> 
     </shape> 
    </item> 
</layer-list> 

填充也適用於每一個複選標記的圖像:

無效:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item 
      android:drawable="@drawable/radio" 
      android:left="6dp" 
      android:right="6dp"/> 
</layer-list> 

活動:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item 
      android:drawable="@drawable/radioact" 
      android:left="6dp" 
      android:right="6dp"/> 
</layer-list> 

和選擇:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/active" android:state_checked="true"/> 
    <item android:drawable="@drawable/inactive" android:state_checked="false"/> 
</selector> 
+0

邊框之間的填充。 http://i.stack.imgur.com/0RRS0.png – ketan

+0

你的意思是這樣的:' < item android:drawable =「@ drawable/radioact」android:left =「6dp」android:right =「6dp」android:state_checked =「true」/> ' – ketan

+0

關閉。但我認爲需要分割圖像和選擇器文件。查看更新的答案。 –

0

使用您RadioButton周圍的佈局和設置backgroundDrawable與自定義邊框

創建border.xml,放入drawable

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <stroke 
     android:width="1dip" 
     android:color="@android:color/black" /> 
    <corners android:radius="10dp"/> 
</shape> 

,然後在Layout

<RelativeLayout 
    android:background="@drawable/border"> 
    // your radiogroup 
</RelativeLayout> 
+0

它使邊框整個無線電集團。我只想要特定的單選按鈕 – ketan

+0

@ketan那麼,然後把你的背景可繪製直接到你的'RadioButton' –

+0

然後邊框將顯示在文本和圖標上方。我可以在圖標上方顯示邊框和圖標 – ketan