为了账号安全,请及时绑定邮箱和手机立即绑定

Android 中自定义长方形

标签:
Android

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 属性文件

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <declare-styleable name="changfx">

        <attr name="widths" format="dimension" />

        <attr name="heights" format="dimension" />

        <attr name="line_left" format="boolean" />

        <attr name="line_top" format="boolean" />

        <attr name="line_right" format="boolean" />

        <attr name="line_bottom" format="boolean" />

        <attr name="color_left" format="color" />

        <attr name="color_top" format="color" />

        <attr name="color_right" format="color" />

        <attr name="color_bottom" format="color" />

        <attr name="bg_color" format="color" />

        <attr name="titleText" format="string" />

    </declare-styleable>

</resources>

main_activity wenjian

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    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" >

    <com.example.view.ChFangX

        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" />

</RelativeLayout>

代码贴完

原文链接:http://www.apkbus.com/blog-651648-59910.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消