问题
I am writing an application where I need to draw two radio button in equal horizontal space. That I achieved by set android:layout_weight="1" property for both of radio button.
Now I need to display custom drawable button in centre and need to place text inside of that drawable, like this:
Here behaviour of both draw drawable and text is dynamic.
Is there any way to achieve both things in centre.
回答1:
Give a try to the following code to achieve the desired output.
<?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>
Below is the code for 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 is the color code which you would like to use.
来源:https://stackoverflow.com/questions/26710100/display-custom-drawable-and-text-in-center-of-radio-button