Android开发:如何自定义带文本的单选按钮?

4
我已经为我的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}

我做错了什么?


有趣;我也正在接触安卓。看起来异常是 java.lang.NoSuchMethodException: RadioButtonText(Context,AttributeSet),但我在你的代码中看到了该构造函数定义... - Ed S.
是的,在我有该方法之前就已经明白了,所以我添加了它,但错误仍然存在。 - Kyle V.
我想我在这里看到了问题... - Ed S.
2个回答

2

RadioButtonTextBlockBallLevelSelect的内部类。如果没有现有的外部类对象,您无法实例化它。因此,在XML中引用该类型,您需要将RadioButtonText标记为static

另外需要注意的是,由于它将是静态的,将其保留为内部类不再合适。


0
radioButton是TextView的子类,图片只是左边的可绘制对象,所以也许更容易移除左侧的可绘制对象,然后使用底部的可绘制对象。其余的样式与任何TextView一样,因此可绘制对象的间距、内边距和外边距都适用。
<RadioButton
            android:id="@+id/level"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@null"
            android:drawableBottom="@android:drawable/btn_radio"  
            android:text="1" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接