【发布时间】:2010-04-13 17:43:08
【问题描述】:
我想知道是否有办法切换文本出现在 android 单选按钮上的一侧?
【问题讨论】:
-
可以用文本和未标记的单选按钮制作复杂的布局
我想知道是否有办法切换文本出现在 android 单选按钮上的一侧?
【问题讨论】:
我想做同样的事情,而不必扩展另一个类(或两个,因为您至少必须扩展 CompoundButton 和 RadioButton)以获得应该是实现的一部分的东西从一开始就。由于我使用的是 RadioGroup,如果您将 RadioButton 和 TextView 放在布局容器中,它将无法工作。诚然,我的解决方案有点老套,但是 - 它有效。
1) 将 Padding left 设置为 40 2) 设置布局左边距为-36dp
此时,原始单选按钮将位于视图之外,您的文本视图将位于最左侧,留有 4dp 的边距。
3) 将 Drawable 权限设置为 @android:drawable/btn_radio
您现在将拥有一个本机 RadioButton,左边是文本,右边是一个按钮,它将与 RadioGroup 一起使用。
@CommonsWare
值得一提的是,在回答这个特定问题时提出人机界面指南非常具有讽刺意味。特别是考虑到调整 RadioButton 布局以将按钮放置在最右侧,将与 Spinner 菜单的布局保持一致。我完全同意你对此事的看法——但 NickTFried 很可能在这方面试图弥补 Android 的“挂起”。
【讨论】:
正如 Ravi Vyas 所指出的,您可以使用 TextView 和 RadioButton 自己执行此操作。从我对源代码的阅读来看,RadioButton 没有任何内在的东西可以相对于文本重新定位按钮。
另外,请记住,仅仅因为这是可能的并不意味着这是一个好主意。例如,在 iPhone 上,如果你在这方面搞得太多,你可能不会被允许发布你的应用程序,因为它们有应用程序必须遵守的人机界面准则。 Android 给了你更多的绳索——不要让你的用户被它束缚。
【讨论】:
如果您使用单个“单选按钮”,CheckedTextView 可以正常工作。
但我需要保留组中单选按钮之间的切换功能,这样对我来说效果更好:
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:gravity="left|center_vertical"
android:layout_marginLeft="-32dp"
android:text="Radio Button 1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:gravity="left|center_vertical"
android:layout_marginLeft="-32dp"
android:text="Radio Button2" />
</RadioGroup>
根据需要调整android:layout_marginLeft。
【讨论】:
对于 RTL 语言,即 Arabic,请使用:
<RadioGroup
android:id="@+id/rgNewsFilter"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/rbAllNews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="-32dp"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:gravity="right|center_vertical"
android:text="ذنيسبمنشخصث"
android:textColor="#ffffff" />
<RadioButton
android:id="@+id/rbMyTeam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="-32dp"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:gravity="right|center_vertical"
android:text="تشسيبتسيتبتسيب"
android:textColor="#ffffff" />
</RadioGroup>
【讨论】:
这可以使用CheckedTextView 来完成;见android RadioButton option on the right side of the text
【讨论】:
Android 有一个可以使用的内置布局 - android.R.layout.simple_list_item_single_choice:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" />
如果您想修改它,您可以将上面的内容复制粘贴到一个新的 XML 文件中,然后使用<include> 标签将其包含在您的其他布局中(在Apache 2.0 License 下)。您也可以将其用作列表项,并保持不变。
【讨论】: