Android 自定义控件方法

Android 自定义控件方法

1、定义View的资源属性。一般在res/values/下建立一个attrs.xml,在xml中定义属性名字及属性类型
举例:
<?xml version="1.0" encoding="utf-8"?>
<resources>


<attr name="titleText" format="string" />
<attr name="titleTextColor" format="color" />
<attr name="titleTextSize" format="dimension" />


<declare-styleable name="CustomTitleView">
<attr name="titleText" />
<attr name="titleTextColor" />
<attr name="titleTextSize" />
</declare-styleable>


</resources>

PS:Attrs format类型有
一般型:
"reference" //引用
"color" //颜色
"boolean" //布尔值
"dimension" //尺寸值
"float" //浮点值
"integer" //整型值
"string" //字符串
"fraction" //百分数,比如200%
枚举型:
< attr name="orientation">
< enum name="horizontal" value="0" />
< enum name="vertical" value="1" />
< /attr>
使用 android:orientation = "vertical"
标志位、位或运算:
< attr name="windowSoftInputMode">
< flag name = "stateUnspecified" value = "0" />
< flag name = "stateUnchanged" value = "1" />
< flag name = "stateHidden" value = "2" />
< flag name = "stateAlwaysHidden" value = "3" />
< flag name = "stateVisible" value = "4" />
< flag name = "stateAlwaysVisible" value = "5" />
< flag name = "adjustUnspecified" value = "0x00" />
< flag name = "adjustResize" value = "0x10" />
< flag name = "adjustPan" value = "0x20" />
< flag name = "adjustNothing" value = "0x30" />
< /attr>
使用 android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
混合型:
< attr name = "background" format = "reference|color" />
使用 android:background = "@drawable/图片ID|#00FF00"

2、编写自定义控件View类代码。
举例(带注释):
package com.example.hellojni.view;


import com.example.hellojni.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;


public class CustomTitleView extends View {

private String mTitleText;
private int mTitleTextColor;
private int mTitleTextSize;

private Rect mBound;
private Paint mPaint;


public CustomTitleView(Context context) 
{
this(context, null);
}

public CustomTitleView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}

public void setTitleText(String t)
{
mTitleText = t;
}

public CustomTitleView(Context context, AttributeSet attrs, int defstyle)
{
super(context, attrs, defstyle);

// 获取自定义属性值过程
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defstyle, 0);
int n = a.getIndexCount();
for(int i = 0; i < n; i++)
{
int attr = a.getIndex(i);
switch (attr) 
{
case R.styleable.CustomTitleView_titleText:
mTitleText = a.getString(attr);
break;
case R.styleable.CustomTitleView_titleTextColor:
mTitleTextColor = a.getColor(attr, Color.BLACK);
break;
case R.styleable.CustomTitleView_titleTextSize:
mTitleTextSize = a.getDimensionPixelSize(attr, (int)TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
break;
default:
break;
}
}
a.recycle();

mPaint = new Paint();
mPaint.setTextSize(mTitleTextSize);

mBound = new Rect();
mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
}


/*
* 自定义view绘制过程逻辑
* 1、测量--onMesure():决定View的大小
* 2、布局--onLayout():决定View在ViewGroup中的位置
* 3、绘制--onDraw():如何绘制这个View
*/
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
// 自定义控件绘制过程
mPaint.setColor(Color.YELLOW);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);

mPaint.setColor(mTitleTextColor);
canvas.drawText(mTitleText, getWidth()/2-mBound.width()/2, 
getHeight()/2+mBound.height()/2, mPaint);
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);

/*
* MeasureSpec封装了父布局传递给子布局的布局要求,每个MeasureSpec代表了一组宽度和高度的要求 
* MeasureSpec由size和mode组成。 
* 三种Mode: 
* 1.UNSPECIFIED 
* 父没有对子施加任何约束,子可以是任意大小(也就是未指定) 
* (UNSPECIFIED在源码中的处理和EXACTLY一样。当View的宽高值设置为0的时候或者没有设置宽高时,模式为UNSPECIFIED 
* 2.EXACTLY 
* 父决定子的确切大小,子被限定在给定的边界里,忽略本身想要的大小。 
* (当设置width或height为match_parent时,模式为EXACTLY,因为子view会占据剩余容器的空间,所以它大小是确定的) 
* 3.AT_MOST 
* 子最大可以达到的指定大小 
* (当设置为wrap_content时,模式为AT_MOST, 表示子view的大小最多是多少,这样子view会根据这个上限来设置自己的尺寸)
*/
int width, height;
if(widthMode == MeasureSpec.EXACTLY)
width = widthSize;
else {
mPaint.setTextSize(mTitleTextSize);
mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
float textWidth = mBound.width();
int desired = (int)(getPaddingLeft()+textWidth+getPaddingRight());
width = desired;
}

if(heightMode == MeasureSpec.EXACTLY)
height = heightSize;
else {
mPaint.setTextSize(mTitleTextSize);
mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
float textHeight = mBound.height();
int desired = (int)(getPaddingTop()+textHeight+getPaddingBottom());
height = desired;
}

setMeasuredDimension(width, height);
}
}
3、在layout中增加自定义的控件
举例:
<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.hellojni"
android:layout_width="match_parent"
。。。>
重要:
在布局文件头增加了命名空间 xmlns:custom="http://schemas.android.com/apk/res/com.example.hellojni"
= 之前为别名,可随意定
= 之后为apk包名,参考AndroidManifest中package的属性值

添加自定义控件代码:
   <com.example.hellojni.view.CustomTitleView
        android:id="@+id/diy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        custom:titleText="3712"
        android:padding="10dp"
        custom:titleTextColor="#ff0000"
        android:layout_centerInParent="true"
        custom:titleTextSize="40sp" 
        android:layout_below="@id/button" />

其中custom:xxx custom即为自定义的别名。
—————————————————————————————————————————————————————————————————————————————
上文自定义控件继承自View,通过onDraw绘制出控件,


实际项目时,可以继承layout,通过纯代码布局,实现由多种控件集成在一起的自定义控件
参考:http://blog.csdn.net/gf771115/article/details/8237229
—————————————————————————————————————————————————————————————————————————————
复杂自定义控件实例参考:http://mobile.51cto.com/aprogram-452154.htm
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值