在单选按钮的中央显示自定义可绘制和文本

时间:2014-11-03 08:07:05

标签: android android-layout radiobuttonlist android-radiogroup

我正在编写一个应用程序,我需要在相等的水平空间中绘制两个单选按钮。我通过为单选按钮设置 android:layout_weight =" 1" 属性来实现。

现在我需要在中心显示自定义可绘制按钮,并且需要在该drawable中放置文本,如下所示:

enter image description here

这里绘制drawable和text的行为都是动态的。

有没有办法在中心实现这两件事。

1 个答案:

答案 0 :(得分:1)

尝试使用以下代码来实现所需的输出。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="horizontal">

   <LinearLayout
       android:orientation="horizontal"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:gravity="center">

       <RadioButton
           android:id="@+id/radioButton"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:background="@drawable/rd_bg"
           android:button="@null"
           android:gravity="center"
           android:text="15"/>
   </LinearLayout>

   <LinearLayout
       android:orientation="horizontal"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:gravity="center">

       <RadioButton
           android:id="@+id/radioButton2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:button="@null"
           android:background="@drawable/rd_bg"
           android:gravity="center"
           android:text="30"/>
   </LinearLayout>
</LinearLayout>

以下是drawable duration_selector的代码

<?xml version="1.0" encoding="utf-8"?>

 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_checked="true"
      android:state_focused="true">
    <shape android:shape="oval">
        <solid android:color="@color/login_bg"/>
        <size
            android:width="50dp"
            android:height="50dp"/>
    </shape>
</item>

<item
    android:state_checked="false"
    android:state_focused="true">
    <shape android:shape="oval">
        <solid android:color="@color/login_bg"/>
        <size
            android:width="50dp"
            android:height="50dp"/>
    </shape>
</item>

<item
    android:state_checked="true">
    <shape android:shape="oval">
        <solid android:color="@color/login_bg"/>
        <size
            android:width="50dp"
            android:height="50dp"/>
    </shape>
</item>

<item
    android:state_checked="false">
    <shape android:shape="oval">
        <solid android:color="@android:color/transparent"/>
        <stroke android:color="@color/login_bg"
                android:width="1dp"/>
        <size
            android:width="50dp"
            android:height="50dp"/>
    </shape>
</item>

@ color / login_bg是您想要使用的颜色代码。