我是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
...根据屏幕大小的不同,文本可以分成不同的行数。它不总是标准的
5条答案
按热度按时间c9x0cxw01#
@style/TabStyle
只是一个简单的样式,你可以忽略它。@drawable/main_selector
是一个图形,根据情况切换。你可以在这里阅读更多关于选择器的信息。将文本置于顶部的示例:
将给予以下结果:
如果希望文本显示在按钮上方,可以使用以下xml:
这将给予以下结果:
o3imoua42#
为了补充Warpzit great answer,您应该在按钮上使用
android:gravity="center_horizontal|bottom"
和android:layout_height="match_parent"
来对齐它们。也没有必要从AOSP复制单选按钮绘图,使用
?android:attr/listChoiceIndicatorSingle
。XML布局
rkue9o1l3#
一个很好的方法是使用一个样式来使这个效果更容易应用。要做到这一点,把这个代码放在你的
styles.xml
上,在resources
标签下。然后,将其应用于RadioButton,如下所示:
dced5bon4#
我最终使用的最简单的解决方案是:
它还使用较新样式的动画图标
listChoiceIndicatorSingle
,这是默认设置。qhhrdooz5#
应在单选按钮中添加android:button="@null”