【发布时间】:2014-08-25 18:08:30
【问题描述】:
我想改变我的按钮形状但我想使用 onDaw 方法和 EXTENDING 按钮 班级。所以我刚开始做的是:
<view class = "com.example.button.MainActivity$MyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
和
public static class MyButton extends Button {
public MyButton(Context context) {
super(context);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(10);
paint.setStyle(Style.FILL);
int width = getWidth();
int height = getHeight();
canvas.drawCircle(width/2, height/2, height/3, paint);
}
}
但我在按钮视图矩形上得到一个蓝色圆圈。
我想看到只是蓝色圆圈作为按钮形状,我得到了矩形的骑乘。 有什么帮助吗??
【问题讨论】:
-
你忘记了 super.onDraw(canvas) 调用。
-
是的,但还是同样的问题。 . .
-
我可能在这方面有很长的路要走,但请尝试将 xml 中的背景设置为透明。
-
其实这是我项目的主要思路,如果我将按钮的背景改为透明的围绕圆圈点击可能会调用按钮点击方法。
-
您的 getWidth 和 getHeight 方法有误。使用 canvas.getWidth 和 canvas.getHeight
标签: android android-view android-custom-view android-button