Android 开发:带有文本的自定义单选按钮?
我为我的 Android 应用程序创建了一个自定义 RadioButton,它只是用自定义图像替换了标准单选按钮。现在我想让通常出现在标准按钮右侧的文本标签与中心的自定义按钮重叠。
有办法做到这一点吗?
更新:这是我尝试创建一个自定义组件来执行此操作:
public class RadioButtonText extends RadioButton {
Paint myPaint = new Paint();
public RadioButtonText(Context context) {
super(context);
}
public RadioButtonText(Context context, AttributeSet attrbs) {
super(context, attrbs);
}
@Override
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
String myText = (String) getText();
canvas.drawText(myText, 10, 10, myPaint);
}
}
这是我在layout.xml中使用它:
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="@drawable/bb_button"
android:id="@+id/levelButton0"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="1">
</view>
但是当我尝试运行该应用程序时,我收到此错误:
06-11 22:16:32.642: ERROR/AndroidRuntime(323): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.createView(LayoutInflater.java:503)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.app.Activity.setContentView(Activity.java:1647)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at com.stickfigs.blockball.BlockBallLevelSelect.onCreate(BlockBallLevelSelect.java:30)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): ... 11 more
06-11 22:16:32.642: ERROR/AndroidRuntime(323): Caused by: java.lang.NoSuchMethodException: RadioButtonText(Context,AttributeSet)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at java.lang.Class.getMatchingConstructor(Class.java:660)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at java.lang.Class.getConstructor(Class.java:477)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.createView(LayoutInflater.java:475)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): ... 23 more
06-11 22:16:32.662: WARN/ActivityManager(42): Force finishing activity com.stickfigs.blockball/.BlockBallLevelSelect
06-11 22:16:33.198: WARN/ActivityManager(42): Activity pause timeout for HistoryRecord{43edc648 com.stickfigs.blockball/.BlockBallLevelSelect}
我做错了什么?
I have created a custom RadioButton for my Android app which just replaces the standard radio button with custom images. Now I want to have the text label that would usually appear to the right of the standard button appear overlapping the custom button in it's center.
Is there a way to do this?
UPDATE: Here is my attempt at create a custom component to do this:
public class RadioButtonText extends RadioButton {
Paint myPaint = new Paint();
public RadioButtonText(Context context) {
super(context);
}
public RadioButtonText(Context context, AttributeSet attrbs) {
super(context, attrbs);
}
@Override
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
String myText = (String) getText();
canvas.drawText(myText, 10, 10, myPaint);
}
}
And here is my using it in my layout.xml:
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="@drawable/bb_button"
android:id="@+id/levelButton0"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="1">
</view>
But when I try to run the app I get this error:
06-11 22:16:32.642: ERROR/AndroidRuntime(323): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.createView(LayoutInflater.java:503)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.app.Activity.setContentView(Activity.java:1647)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at com.stickfigs.blockball.BlockBallLevelSelect.onCreate(BlockBallLevelSelect.java:30)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): ... 11 more
06-11 22:16:32.642: ERROR/AndroidRuntime(323): Caused by: java.lang.NoSuchMethodException: RadioButtonText(Context,AttributeSet)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at java.lang.Class.getMatchingConstructor(Class.java:660)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at java.lang.Class.getConstructor(Class.java:477)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): at android.view.LayoutInflater.createView(LayoutInflater.java:475)
06-11 22:16:32.642: ERROR/AndroidRuntime(323): ... 23 more
06-11 22:16:32.662: WARN/ActivityManager(42): Force finishing activity com.stickfigs.blockball/.BlockBallLevelSelect
06-11 22:16:33.198: WARN/ActivityManager(42): Activity pause timeout for HistoryRecord{43edc648 com.stickfigs.blockball/.BlockBallLevelSelect}
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
radioButton 是 TextView 的子类,图像只是左侧可绘制,因此删除左侧可绘制并使用底部可绘制可能更容易。其余的样式与任何文本视图一样,因此适用drawablePadding、paddings 和margin。
The radioButton is a subclass of TextView, the image it's just the left drawable, so maybe it's easier to remove the drawable left and use the drawable bottom. The rest of styling is as any textview, so drawablePadding, paddings and margins applies.
RadioButtonText
是BlockBallLevelSelect
的内部类。如果没有现有的外部类对象,则无法实例化它的实例。因此,您需要将RadioButtonText
标记为static
以引用 XML 中的类型。附带说明一下,由于它将是静态的,因此将其保留为内部类不再有意义。
RadioButtonText
is an inner class ofBlockBallLevelSelect
. you cannot instantiate an instance of it without an existing outer class object. As such, you will need to markRadioButtonText
asstatic
to reference the type in XML.As a side note, since it will be static it no longer makes a lot of sense to leave it as an inner class.