Android Dev:带文字的自定义单选按钮?

时间:2011-06-11 08:15:53

标签: java android xml

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

我做错了什么?

2 个答案:

答案 0 :(得分:2)

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

作为旁注,因为它将是静态的,所以将它留作内部阶段不再有意义。

答案 1 :(得分:0)

radioButton是TextView的子类,它只是左侧可绘制的图像,因此可能更容易删除可绘制的左侧并使用可绘制的底部。其余样式与任何textview一样,因此drawablePadding,paddings和margin应用。

<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" />