Android之自定义Button控件

来源:互联网 发布:淘宝店更换类目影响 编辑:程序博客网 时间:2024/06/03 18:00

1、首先我们创一个Java类,自己命名,然后在里面进行相关的设计

public class MyButton extends View {        public MyButton(Context context) {          super(context);      }        public MyButton(Context context, @Nullable AttributeSet attrs) {          super(context, attrs);      }        public MyButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {          super(context, attrs, defStyleAttr);      }        public MyButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {          super(context, attrs, defStyleAttr, defStyleRes);      }        @Override      protected void onDraw(Canvas canvas) {          super.onDraw(canvas);          //设置背景颜色          canvas.drawColor(Color.GRAY);          //实例化路径          Path path=new Path();          path.moveTo(100,100);          path.lineTo(100,400);          path.lineTo(400,400);          path.lineTo(100,100);                //实例化一个画笔          Paint paint=new Paint();          paint.setColor(Color.RED);            //设置空心          paint.setStyle(Paint.Style.STROKE);          //设置画笔的粗细          paint.setStrokeWidth(12);            //画路径          canvas.drawPath(path,paint);            //设置画笔的粗细          paint.setStrokeWidth(3);          paint.setColor(Color.BLACK);          //设置字体的大小          paint.setTextSize(50);          //画文字          canvas.drawText("按钮",150,350,paint);        }  
2、设置画布的宽和高

只用重写onMeasure这个方法,然后设置它的宽高就可以控制这个控件了

 @Override      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {          super.onMeasure(widthMeasureSpec, heightMeasureSpec);          //设置画布的宽和高          setMeasuredDimension(450,450);      }  

3、然后在xml文件使用我们自己写的Button

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:app="http://schemas.android.com/apk/res-auto"      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"      android:layout_height="match_parent"      tools:context="com.zking.laci.android16_diyui.MainActivity"      android:orientation="vertical"      >        <com.zking.laci.android16_diyui.MyButton          android:layout_width="match_parent"          android:layout_height="match_parent"          android:id="@+id/mybtn_main"          android:layout_gravity="center_horizontal"          />    </LinearLayout>  
3、最后在相对应的Activity中来设置监听检验

public class MainActivity extends AppCompatActivity {        private MyButton myButton;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          //获得该ID的控件          myButton = (MyButton) findViewById(R.id.mybtn_main);          //给该控件设置监听          myButton.setOnClickListener(new View.OnClickListener() {              @Override              public void onClick(View v) {                  Toast.makeText(MainActivity.this, "点击了", Toast.LENGTH_SHORT).show();              }          });      }  }  





原创粉丝点击