我在Android应用程序中创建单选按钮,但我想使它自定义.但我不知道如何做到这一点.我希望图像作为图标和图标下的文本,如在此图像所示.此外,我想改变颜色,每当任何单选按钮被选中.
的数据我想要这种类型的单选按钮。请告诉我怎么做?
0kjbasz61#
您可以使用自定义背景
<RadioButton android:id="@+id/radioButtom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:button="@drawable/custom_background" android:checked="true" android:text="Your Text" />
<RadioButton
android:id="@+id/radioButtom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:button="@drawable/custom_background"
android:checked="true"
android:text="Your Text" />
字符串Custom_background.xml
Custom_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" ><item android:drawable="@drawable/your_regular_state_drawable" android:state_checked="true" android:state_pressed="true" /><item android:drawable="@drawable/your_selected_state_drawable" android:state_pressed="true" /><item android:drawable="@drawable/your_selected_state_drawable" android:state_checked="true" /><item android:drawable="@drawable/your_regular_state_drawable" />
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/your_regular_state_drawable"
android:state_checked="true"
android:state_pressed="true" />
android:drawable="@drawable/your_selected_state_drawable"
android:state_checked="true" />
android:drawable="@drawable/your_regular_state_drawable" />
型参考:你可以从这里得到更好的描述https://stackoverflow.com/a/19163987/12676247
ej83mcc02#
<RadioGroup android:id="@+id/radio_group_do_you_smoke" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="16dp" android:background="@drawable/transparent_drawable" android:orientation="horizontal" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView119"> <RadioButton android:id="@+id/radio_button_yes_1" style="@style/BoldTextView" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/rbtn_selector" android:button="@null" android:checked="false" android:foreground="@drawable/smoke_ic" android:foregroundGravity="center" android:gravity="center" android:textAlignment="center" android:textColor="@drawable/rbtn_textcolor_selector" android:textSize="14dp" tools:ignore="TextSizeCheck"> </RadioButton> </RadioGroup>
<RadioGroup
android:id="@+id/radio_group_do_you_smoke"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:background="@drawable/transparent_drawable"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView119">
android:id="@+id/radio_button_yes_1"
style="@style/BoldTextView"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/rbtn_selector"
android:button="@null"
android:checked="false"
android:foreground="@drawable/smoke_ic"
android:foregroundGravity="center"
android:gravity="center"
android:textAlignment="center"
android:textColor="@drawable/rbtn_textcolor_selector"
android:textSize="14dp"
tools:ignore="TextSizeCheck">
</RadioButton>
</RadioGroup>
字符串你可以使用android:foreground和android:foregroundGravity=“center”image
2条答案
按热度按时间0kjbasz61#
您可以使用自定义背景
字符串
Custom_background.xml
型
参考:你可以从这里得到更好的描述https://stackoverflow.com/a/19163987/12676247
ej83mcc02#
字符串
你可以使用android:foreground和android:foregroundGravity=“center”
image