android 按钮上方带有文本的单选按钮

5cg8jx4n  于 2022-12-16  发布在  Android
关注(0)|答案(5)|浏览(184)

我是Android新手,我需要在我的Activity上添加单选按钮,但我需要将文本放置在项目符号按钮的To上。
我发现了以下内容,虽然我不明白@drawable/main_selector和@style/TabStyle是什么。
Radiobutton with text on top
谁能给予我一个101指南。

更新

我根据一些建议使用了以下方法,但没有效果:

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
    android:text="something that is on top"
    android:id="@+id/toggle_tab_left"
    android:button="?android:attr/listChoiceIndicatorSingle"        
    style="@null"/>

<RadioButton 
    android:button="?android:attr/listChoiceIndicatorSingle"
    android:text="something that is on top"
    android:id="@+id/toggle_tab_right"
    style="@null"/>
</RadioGroup>

更新2

我得到了我的解决方案从Warpzit,但befor我标记为回答的问题,有人能帮助我在下面的对齐问题。我将有5个单选按钮在一行,其中一些将有较长的文本分为2行。当文本适合在屏幕上,因为横向,或在平板电脑上,然后所有的文本应该在一行:

更新3

...根据屏幕大小的不同,文本可以分成不同的行数。它不总是标准的

c9x0cxw0

c9x0cxw01#

@style/TabStyle只是一个简单的样式,你可以忽略它。@drawable/main_selector是一个图形,根据情况切换。你可以在这里阅读更多关于选择器的信息。
将文本置于顶部的示例:

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
    android:text="Text on top"
    android:button="@null"
    android:background="#f00"
    android:layout_weight="1"/>

<RadioButton 
    android:text="Text on top"
    android:button="@null"
    android:background="#0f0"
    android:layout_weight="1"/>
</RadioGroup>

将给予以下结果:

如果希望文本显示在按钮上方,可以使用以下xml:

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
    android:text="Text on top"
    android:button="@null"
    android:drawableBottom="@android:drawable/btn_radio"
    android:gravity="center"
    android:layout_weight="1"/>

<RadioButton
    android:text="Text on top"
    android:button="@null"
    android:drawableBottom="@android:drawable/btn_radio"
    android:gravity="center"
    android:layout_weight="1"/>
</RadioGroup>

这将给予以下结果:

o3imoua4

o3imoua42#

为了补充Warpzit great answer,您应该在按钮上使用android:gravity="center_horizontal|bottom"android:layout_height="match_parent"来对齐它们。
也没有必要从AOSP复制单选按钮绘图,使用?android:attr/listChoiceIndicatorSingle

XML布局

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radioGroup1"
    android:layout_width="400dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="20dp" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:button="@null"
        android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
        android:gravity="center_horizontal|bottom"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:button="@null"
        android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
        android:gravity="center_horizontal|bottom"
        android:text="RadioButton dsfsdfsdfsdfsdf" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:button="@null"
        android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
        android:gravity="center_horizontal|bottom"
        android:text="RadioButton fdsfsd fdsfsdf fsfsdfs" />

</RadioGroup>
rkue9o1l

rkue9o1l3#

一个很好的方法是使用一个样式来使这个效果更容易应用。要做到这一点,把这个代码放在你的styles.xml上,在resources标签下。

<style name="RadioWithTextOnTop">
    <item name="android:button">@null</item>
    <item name="android:drawableBottom">?android:attr/listChoiceIndicatorSingle</item>
    <item name="android:gravity">center_horizontal|bottom</item>
</style>

然后,将其应用于RadioButton,如下所示:

<RadioButton
    style="@style/RadioWithTextOnTop"
    android:id="@+id/radioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="RadioButton" />
dced5bon

dced5bon4#

我最终使用的最简单的解决方案是:

<RadioButton
    ...
    android:gravity="center_horizontal|bottom"
    android:drawableTop="?android:attr/listChoiceIndicatorSingle"
    android:button="@null"
    />

它还使用较新样式的动画图标listChoiceIndicatorSingle,这是默认设置。

qhhrdooz

qhhrdooz5#

<RadioButton
                    android:id="@+id/radio1"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:button="@null"
                    android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
                    android:gravity="center_horizontal|bottom"[enter image description here][1]
                    android:background="@drawable/app_border_0"
                    android:padding="@dimen/_15sdp"
                    android:text="@string/no"
                    android:fontFamily="@font/poppins_medium"
                    style="@style/AppRadioAppStyle"
                    android:layout_marginStart="@dimen/_10sdp"/>

应在单选按钮中添加android:button="@null”

相关问题