android 自定义控件有几种方式

今天是我第一次写博客,也是第一次学习写自定义控件,不足的地方望大家指正。

同时注意自定义控件的命名空间

下面是代码;

package com.example.changfangxing;

import android.os.Bundle;

import android.support.v7.app.ActionBarActivity;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Toast;

import com.example.view.ChFangX;

public class MainActivity extends ActionBarActivity {

private ChFangX mChFangX;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mChFangX = (ChFangX) findViewById(R.id.chfangx);

mChFangX.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(getApplicationContext(), mChFangX.getText(),

1000).show();

}

});

}

}

package com.example.view;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.view.View;

import com.example.changfangxing.R;

/**

*

*

* @author Administrator

*

*/

public class ChFangX extends View {

// 每个边框的顏色

private int color_left;

private int color_top;

private int color_right;

private int color_bottom;

// 边框的有无

private boolean line_left;

private boolean line_top;

private boolean line_ritght;

private boolean line_bottom;

// 字

private String text;

// 画笔

// 左边线,上边,右边,底边线的画笔

private Paint pain_left, pain_top, pain_right, pain_bottom;

private Paint paintText;

private Float widths;

private Float heighs;

// 各边颜色直

public ChFangX(Context context) {

// TODO Auto-generated constructor stub

this(context, null);

}

public ChFangX(Context context, AttributeSet attrs) {

// TODO Auto-generated constructor stub

this(context, attrs, 0);

}

public ChFangX(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

// TODO Auto-generated constructor stub

TypedArray a = context.obtainStyledAttributes(attrs,

R.styleable.changfx);

color_left = a.getColor(R.styleable.changfx_color_left, Color.BLUE);

color_top = a.getColor(R.styleable.changfx_color_top, Color.BLUE);

color_bottom = a.getColor(R.styleable.changfx_color_bottom, Color.BLUE);

color_right = a.getColor(R.styleable.changfx_color_right, Color.BLUE);

line_left = a.getBoolean(R.styleable.changfx_line_left, false);

line_top = a.getBoolean(R.styleable.changfx_line_top, false);

line_ritght = a.getBoolean(R.styleable.changfx_line_right, false);

line_bottom = a.getBoolean(R.styleable.changfx_line_bottom, false);

text = a.getString(R.styleable.changfx_titleText);

widths = a.getDimension(R.styleable.changfx_widths, 20);

heighs = a.getDimension(R.styleable.changfx_heights, 20);

pain_left = new Paint();

pain_top = new Paint();

pain_right = new Paint();

pain_bottom = new Paint();

paintText = new Paint();

a.recycle();

}

@Override

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

super.onDraw(canvas);

// 初始化画笔

pain_left.setColor(color_left);

pain_top.setColor(color_top);

pain_right.setColor(color_right);

pain_bottom.setColor(color_bottom);

paintText.setColor(Color.BLACK);

// 初始化一个矩形

// Rect mRect = new Rect(5, 5, 200, 100);

if (line_top)

canvas.drawLine(0, 0, widths, 0, pain_top);

if (line_ritght)

canvas.drawLine(widths, 0, widths, heighs, pain_right);

if (line_bottom)

canvas.drawLine(widths, heighs, -widths, heighs, pain_bottom);

if (line_left)

canvas.drawLine(0, heighs, 0, 0, pain_left);

canvas.drawText(text, widths / 2, heighs / 2, paintText);

}

public String getText() {

return text;

}

}

attr 属性文件

main_activity wenjian

xmlns:tools="http://schemas.android.com/tools"

xmlns:custom="http://schemas.android.com/apk/res/com.example.changfangxing"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/chfangx"

android:layout_width="200dp"

android:layout_height="50dp"

android:layout_marginTop="20dp"

custom:color_left="@android:color/holo_red_light"

custom:heights="50dp"

custom:line_bottom="false"

custom:line_left="true"

custom:line_right="true"

custom:line_top="true"

custom:titleText="ok"

custom:widths="200dp" />

代码贴完

android自定义矩形框,Android 中自定义长方形相关推荐

  1. android自定义矩形框,Android中ImageCropper矩形、圆形 裁剪框的实现方法

    前言 支持圆形裁剪框,裁剪后生成圆形图案. 代码基于开源项目修改,github上项目链接:https://www.easck.com/> 说一下圆形裁剪实现部分: 1.UI方面,自定义Circl ...

  2. android 画空心矩形框,Android实现空心圆角矩形按钮的实例代码

    搜索热词 页面上有时会用到背景为空心圆角矩形的Button,可以通过xml绘制出来. drawrable文件夹下bg_red_hollow_rectangle.xml android:shape=&q ...

  3. android摄像头矩形框提取_视频图像摄像头跟踪的上位机系统设计

    ​背景介绍 视频序列中的目标进行跟踪是计算机视觉的热门研究领域之一,其主要过程是首先在视频序列的第一帧图像中选定跟踪目标,之后运行跟踪算法,实现在后续帧中预测跟踪目标的位置.本系统设计了一个集成视觉目 ...

  4. android怎么加矩形框,Android shape 矩形框子

    Android shape 矩形边框 前言         欢迎大家我分享和推荐好用的代码段~~声明         欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...

  5. android 虚线圆角矩形框,android 画虚线、实线,画圆角矩形,一半圆角

    1.画虚线,实线: 建立dotted_line_gray.xml文件放在drawable文件夹下面. android:shape="line" 可以修改你想要的形状 android ...

  6. android天气搜索框,Android简易天气App

    原标题:Android简易天气App 前言 本打算是写一个贝塞尔曲线的demo,想了一下哪种场景可以直观的表现出贝塞尔曲线,想到天气预报中的那些24小时和未来几日天气变化正好适用.接着开始构思,开始是 ...

  7. android+底部评论框,Android 之BottomsheetDialogFragment仿抖音评论底部弹出对话框效果(实例代码)...

    实现的效果图: 自定义Fragment继承BottomSheetDialogFragment 重写它的三个方法: onCreateDialog() onCreateView() onStart() 他 ...

  8. 小程序自定义搜索框_将自定义搜索提供程序添加到Windows 7以及高级搜索技巧

    小程序自定义搜索框 Disclaimer: Many of my tips have crashed cars, broken lights, and caused lawnmowers to go ...

  9. android动态文本框,Android文本框实现搜索和清空效果

    本文实现的效果: 文本框输入为空时显示输入的图标:不为空时显示清空的图标,此时点击清空图标能清空文本框内输入文字. 实现效果: 核心代码: package com.example.test; impo ...

最新文章

  1. Yii学习笔记:利用setFlash和runController打造个性化的提示信息页面
  2. 蚁群算法搜索个体适应度值
  3. 《UML面向对象设计基础》—第1章1.5节消息
  4. [html] 怎样禁用页面中的右键、打印、另存为、复制等功能?
  5. java启动参数_Java启动参数的思考
  6. Hive数据导出的几种方式
  7. 学习做生意的知识重要吗?
  8. 为什么开发环境如此之乱
  9. 第二章 Maxwell 安装部署
  10. Fliqlo屏保mac
  11. 传奇开服教程:传奇添加地图花屏原因与解决方法
  12. NTFS删除及恢复分析
  13. 长文干货! 一文搞懂IoC的依赖注入
  14. 计算机图形学 实验二 三维模型读取与控制【OpenGL】
  15. mov,lea指令的区别
  16. 标准应用促边缘云成熟度提升
  17. 苹果电脑显示内存不足有什么办法解决
  18. DSP电机控制串行通信系统电路设计_电动机控制电路图讲解
  19. ros2 foxy 设置publisher与subscription的qos
  20. LeetCode 1496 - 1499

热门文章

  1. 抽象类(abstract class)和接口(interface)
  2. python判断回文序列_怎么用python3代码检查回文序列?
  3. 进入计算机专业学习的一些体会和思考以及今后的学习规划
  4. sqlplus 为什么会自动断开_为什么设置的手机铃声会自动变为手机默认的,原因在这里...
  5. 一条命令在 Ubuntu 中安装所有基本的媒体编解码器
  6. Servlet+JSP一文完结
  7. 刷车机器人_各位车主注意了!这样的洗车很伤爱车!99%的人都不知道!
  8. 如何查看linux当前版本
  9. 【conda安装】mamba安装 ==conda安装记录
  10. 线性代数(二十四) : 行列式的展开式—拉普拉斯公式