Java Android开发人员:带有文本的自定义单选按钮?
我已经为我的Android应用程序创建了一个自定义单选按钮,它只是用自定义图像替换了标准单选按钮。现在,我想让通常出现在标准按钮右侧的文本标签与位于其中心的自定义按钮重叠 有办法做到这一点吗 更新:以下是我创建自定义组件的尝试:Java Android开发人员:带有文本的自定义单选按钮?,java,android,xml,Java,Android,Xml,我已经为我的Android应用程序创建了一个自定义单选按钮,它只是用自定义图像替换了标准单选按钮。现在,我想让通常出现在标准按钮右侧的文本标签与位于其中心的自定义按钮重叠 有办法做到这一点吗 更新:以下是我创建自定义组件的尝试: public class RadioButtonText extends RadioButton { Paint myPaint = new Paint(); public RadioButtonText(Context context) {
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);
}
}
下面是我在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>
我做错了什么?
RadioButtonText
是BlockBallLevelSelect
的一个内部类。如果没有现有的外部类对象,则无法实例化它的实例。因此,您需要将radiobuttonext
标记为static
,以引用XML中的类型
作为旁注,由于它将是静态的,因此将其作为一个内部类不再有意义。radioButton是TextView的一个子类,图像只是左侧可绘制的,因此可能更容易删除可绘制的左侧并使用可绘制的底部。样式的其余部分与任何textview一样,因此drawablePadding、paddings和边距都适用
<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" />
有趣;我自己刚进入安卓系统。看起来异常是java.lang.NoSuchMethodException:RadioButtonText(Context,AttributeSet)
,但我看到了代码中定义的构造函数…是的,我在使用该方法之前就得到了它,所以我添加了它,但错误仍然存在。我想我看到了这里的问题。。。
<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" />