使用 RadioGroup/RadioButton 对齐文本 [英] Aligning Text with a RadioGroup/RadioButton
问题描述
我想在 RadioGroup 旁边添加文本,作为它的一种标签.所以 RadioGroup 中有两个带有文本的按钮,目前,我只能让顶部对齐,但这看起来很尴尬.我可以尝试将 TextView 边距对齐,但这并不理想或容易.RadioGroup 和 TextView 生活在一个 RelativeLayout 中,我已经尝试 android:layout_alignBaseline
使用 RadioGroup 和 RadioButtons 之一为 TextView,但似乎 RadioGroup 没有基线,我似乎无法从 TextView 获取 RadioButton 的基线.
I would like to have text next to a RadioGroup that is a sort of label for it. So there are two buttons in the RadioGroup with text to them, and currently, I can only get the tops to align, but that looks pretty awkward. I can try to margin the TextView in line but that's not ideal or easy. The RadioGroup and TextView live inside a RelativeLayout, I've tried android:layout_alignBaseline
for the TextView with the RadioGroup and one of the RadioButtons, but it doesn't seem that the RadioGroup has a baseline, and I can't seem to get the baseline for the RadioButton from the TextView.
推荐答案
这几天我也遇到了同样的问题,我终于解决了.我尝试了我想到的所有组合,最后我发现诀窍是将 android:layout_weight 与 TextViews 上的值相同.这将它们对齐在 RadioButtons 的中心.
I had the same problem these days and I finally solved it. I tried every combination that came on my mind and in the end I figure it out that trick is in putting android:layout_weight with a same value on TextViews. That aligns them just on the center of RadioButtons.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginLeft="15dip"
android:orientation="horizontal" >
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/radio_button"
android:paddingRight="15dip"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/radio_button"
android:paddingRight="15dip"
/>
</RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/cop_radiobutton_label"
android:layout_weight="1"
android:gravity="center_vertical"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/thief_radiobutton_label"
android:layout_weight="1"
android:gravity="center_vertical"/>
</LinearLayout>
</LinearLayout>
这篇关于使用 RadioGroup/RadioButton 对齐文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!