我爲我的Android應用程序創建了一個自定義RadioButton,它只是用自定義圖像替換標準單選按鈕。現在我想讓通常顯示在標準按鈕右側的文本標籤看起來與其中間的自定義按鈕重疊。Android Dev:帶文本的自定義單選按鈕?
有沒有辦法做到這一點?
UPDATE:這是我在創建自定義組件做這樣的嘗試:
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}
我在做什麼錯?
有趣;我只是自己進入android。看起來像是異常是'java.lang.NoSuchMethodException:RadioButtonText(Context,AttributeSet)',但我看到在你的代碼中定義的構造函數... – 2011-06-11 22:26:23
是的,我得到它之前,我有這種方法,所以我加了它,但錯誤仍然存在。 – 2011-06-11 22:32:56
我想我在這裏看到問題... – 2011-06-12 00:08:33