子类按钮采用Android:很多错误 [英] Subclass Button with Android : lots of errors

查看:100
本文介绍了子类按钮采用Android:很多错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想继承按钮,但我有很多错误的发动我的项目时。你可以看看,并告诉我如何解决这一问题? (我有可能50个错误来了)

I tried to subclass Button , but I have a lot of errors when launching my project. Could you have a look and tell me how to fix this? (I've got maybe 50 errors coming up)

package my.project.name;

import android[...]

public class MyButton extends Button {

    public MyButton(){
        super(null);
    }

    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 canvasObject) {
        super.onDraw(canvasObject);
        int x = 100;
        int y = 100;
        int width = 80;
        int height = 200;
        Paint thePaint = new Paint();   
        thePaint.setColor(Color.WHITE);
        RectF rectangle1 = new RectF(x,y,x+width,y+height);
        canvasObject.drawRoundRect(rectangle1, 10.0f, 10.0f, thePaint);   
    }

}

主类:

package my.project.name;

import android.app.Activity;
import android.os.Bundle;

public class MyProjectActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

和XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <MyButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myb"
        android:tag="tag"
         />

</LinearLayout>

编辑:实际上,矩形显示不出来,子类的作品,因为我没有任何错误,但矩形是不可见的,即使有背景@null......

actually, the rectangle does not show up, the subclass works, because i've got no error, but the rectangle is just not visible, even with the background to "@null"...

多谢

推荐答案

改名

<MyButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myb"
    android:tag="tag"
     />

到一个完全合格的命名空间

to a fully qualified Namespace

<namespace.from.button.MyButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myb"
    android:tag="tag"
     />

这篇关于子类按钮采用Android:很多错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆