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(); } }); } }
阅读全文
0 0
- Android之自定义Button控件
- Android开发之自定义控件-自定义Button
- Android系列之自定义Button控件
- Android控件之Button
- Android控件之Button
- Android控件之Button
- Android控件之Button
- android控件之Button
- wpf 自定义控件之Button
- android之自定义button
- 【android自定义控件】button样式自定义<二>
- Android 控件开发之Button
- Android之Button控件学习
- Android 控件开发之Button
- Android控件之二 (Button)
- Button控件+自定义Button控件
- android自定义控件Button 带图片文字
- Android基础知识之控件系列(2)——Button及自定义背景
- LintCoder python 小白2-骰子求和
- PagerSlidingTabStrip源码分析
- 带Attention机制的Seq2Seq框架梳理
- 石子归并问题
- 直方图最大矩形面积--c/c++
- Android之自定义Button控件
- Java中声明变量的八进制与十六进制
- 第四天-Java继承
- CentOS 7装mysql
- python网络数据采集读书笔记0
- 大话PCIe:设备枚举
- 算法-时间复杂度、空间复杂度
- 网络编程常见面试题
- S5PV210学习笔记一