如何在 Android 中为 RadioGroup 制作标题边框?

adr*_*_88 5 android border

如标题所示 - 是否可以在 RadioGroup 周围制作有标题的边框?或者至少是一个简单的边框......谢谢

小智 2

标题边框,我不知道...您可能只想在边框上方添加一个 TextView 小部件。但对于普通边框,以下是我在无线电组周围放置边框的方法: 这是我的基本无线电组(具有三个按钮,水平)

<RadioGroup android:id="@+id/myRadioLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="38dp"
    android:orientation="horizontal"
    android:elevation="24dp">

    <RadioButton android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioBtn1"
        android:text="RadioButton1" android:checked="true"
        style="@android:style/Widget.CompoundButton.RadioButton"
        android:textSize="14sp">
    </RadioButton>



    <RadioButton android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioBtn2"
        android:text="RadioButton2"
        style="@android:style/Widget.CompoundButton.RadioButton"
        android:textSize="14sp">
    </RadioButton>

    <RadioButton android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioBtn3"
        android:text="RadioButton3" android:checked="true"
        style="@android:style/Widget.CompoundButton.RadioButton"
        android:textSize="14sp">
    </RadioButton>  
</RadioGroup>
Run Code Online (Sandbox Code Playgroud)

因此,如果我想要一个围绕我的单选按钮组的边框,我会将一个 xml 文件添加到我的可绘制文件夹中。我们将其称为“border.xml”。这是 border.xml 的代码:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:width="5px" android:color="#000000" />
</shape>
Run Code Online (Sandbox Code Playgroud)

然后,将以下代码添加到单选组 xml 中:

<RadioGroup android:id="@+id/myRadioLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="38dp"
    android:orientation="horizontal"
    android:background="@drawable/border"
    android:elevation="24dp">
Run Code Online (Sandbox Code Playgroud)

您的无线电组周围应该出现一个边框。请注意,您可以改变属性以获得不同形状的边框、渐变、圆角等。形状元素的可能属性可以在此处找到:

https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape