学习自定义View

实现自定义View需要完成三个目标:

1:设计需要的属性;

2:实现一个我们的View;

3:引用我们的View

那么就一步步在解说。

1:设计需要的属性

设计我们需要的属性需要写在values里面,那么我们起个名字叫atts,然后在里面写:

具体代码是:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Tobbar">
        <attr name="title" format="string"/>
        <attr name="titleTextSize" format="dimension"/>
        <attr name="titleColor" format="color"/>
        <attr name="textcolor" format="color"/>
        <attr name="leftTextcolor" format="color"/>
        <attr name="leftbackground" format="reference|color"/>
        <attr name="leftText" format="string"/>
        <attr name="rightTextcolor" format="color"/>
        <attr name="rightbackground" format="reference|color"/>
        <attr name="rightText" format="string"/>
    </declare-styleable>
</resources>
那么解释一下,resources是本来就有的,我们写的东西都会包含这里面,我们要做的就是写一个

<declare-styleable name="X">

       <attr name="Y" format="Z"/>

       ...

</declare-styleable>

这里的<declare-styleable>表示是我们自定义的属性部分,那么里面的<attr name="Y" format="Z"/>就是具体的属性名称和类型,

其中的X表示我们自定义属性的总名称,Y表示一个属性,Z表示Y的类型,例如第一个

<attr name="title" format="string"/>我们定义了一个title属性,并且我们给这个属性一个类型,是一个字符串类型,其他亦然。
读者这样就不难看出来我上面写的这些代码其实就是包含了一个标题和两个按钮的属性。
到这里其实我们就完成了第一步,这就是自定义属性的过程。
 
2:实现一个我们的View
实现一个我们的View其实也不难,先找到我们MainActivty的包,我们在这个包下新建一个java文件,起名叫Tobbar,Tobbar好理解,就是标题栏的意思。
全部代码附上:
 
第一部分:
package org.crazyit.ui.practise;                    

import android.content.Context;                     
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RelativeLayout;
import android.widget.Button;
import android.widget.TextView;
/**
 * Created by Mr-x on 2016/10/08.
 */

public class Tobbar extends RelativeLayout {
第二部分:
    private Button leftbutton, rightbutton;
    private TextView tvtitle;
    private int leftTextColor;
    private Drawable leftBackground;
    private String leftText;;
    private int rightTextColor;
    private Drawable rightBackground;
    private String rightText;
    private float titleTextSize;
    private int titleTextColor;
    private String title;
第三部分:
    private LayoutParams leftparams, rightparams, titleparams;
第四部分:
    public Tobbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Tobbar);
        leftTextColor = ta.getColor(R.styleable.Tobbar_leftTextcolor, 0);
        leftBackground = ta.getDrawable(R.styleable.Tobbar_leftbackground);
        leftText = ta.getString(R.styleable.Tobbar_leftText);
        rightTextColor = ta.getColor(R.styleable.Tobbar_rightTextcolor, 0);
        rightBackground = ta.getDrawable(R.styleable.Tobbar_rightbackground);
        rightText = ta.getString(R.styleable.Tobbar_rightText);
        titleTextColor = ta.getColor(R.styleable.Tobbar_titleColor, 0);
        title = ta.getString(R.styleable.Tobbar_title);
        titleTextSize = ta.getDimension(R.styleable.Tobbar_titleTextSize, 0);
第五部分:
        ta.recycle();
        leftbutton = new Button(context);
        rightbutton = new Button(context);
        tvtitle = new TextView(context);
        leftbutton.setText(leftText);
        leftbutton.setBackgroundColor(leftTextColor);
        rightbutton = new Button(context);
        rightbutton.setText(rightText);
        rightbutton.setBackgroundColor(rightTextColor);
        tvtitle.setText(title);
        tvtitle.setTextColor(titleTextColor);
        tvtitle.setTextSize(titleTextSize);
        tvtitle.setGravity(Gravity.CENTER);
        setBackgroundColor(0xFFF59563);
第六部分:

        leftparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
        addView(leftbutton, leftparams);
        rightparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rightparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
        addView(rightbutton, rightparams);
        titleparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        titleparams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
        addView(tvtitle, titleparams);
    }
}
为了读者理解,我把以上代码分成了五个部分,第一部分想必大家都知道,就是导包的部分,这里不做解释,还有就是整个java代码继承RelativeLayout,接下来的一些常量要根据这个书写;
第二部分:是声明变量的部分,不难看出来是声明了两个按钮和他们的TextView还有一个title;
第三部分:
这里是声明了三个集合体,什么集合体呢?控件放置规则的集合体,等下会有介绍;
第四部分:这是一个构造器,注意我们要选有两个参数的这个构造器,因为要用来写自定义View,AttributeSet attrs就是用来准备自定义View的。
重要的是这一句:
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Tobbar);
还记得我们之前写的自定义属性吗,有个自定义属性总称Tobbar,这里就引用,然后有:
leftTextColor = ta.getColor(R.styleable.Tobbar_leftTextcolor, 0);
这里其实就开始把我们自定义的属性拿来用了,有两个参数,前面是我们定义好的属性名,后面我们一般就写0好了。
相信大家能看懂这几个行代码的作用,getColor明显是把自定义颜色的属性拿来用,其他类推。

第五部分:ta.recycle();
这里其实相当于一个资源回收,就是为了防止资源浪费而写的,知道什么意思就可以了。

第六部分:
这里其实我们用到了一个LayoutParams,这个是什么呢?就是用来表示控件放置在界面里面位置及其他属性的一个集合体,什么意思?
看这句话:leftparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
在这句话里面我们设置了两个东西,宽和高,然后赋值给了leftparams,这样的话就弄好了一个放置控件位置的集合,
然后看这句话:leftparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
这里我们设置了leftparams的一个对齐方式,这下懂了没?
最后有一句:addView(leftbutton, leftparams);
这里的意思是把leftbutton这个控件放到布局里面去,并且附带了规则,即leftparams。前后我们总共添加了三个控件。

3:引用我们的View
最后看一下怎么在xml文件中引用我们的自定义View,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cutomer="http://schemas.android.com/apk/res-auto"
    cutomer:android="http://schemas.android.com/apk/res_auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <org.crazyit.ui.practise.Tobbar
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        cutomer:leftbackground="#f00"
        cutomer:leftText="左边"
        cutomer:leftTextcolor="#130e0e"
        cutomer:rightbackground="#ff0000"
        cutomer:rightText="左边"
        cutomer:rightTextcolor="#130e0e"
        cutomer:title="标题"
        cutomer:titleColor="#f00"
        cutomer:titleTextSize="10dp"/>
</RelativeLayout>
是不是有点看不懂,这个cutomer是什么鬼,这其实表示的是一个第三方,即我们创建的东西,是什么呢?想想我们平时应该写android:...什么什么的,现在用cutomer代替了,那么我们就要导入一下包,那么就会有第三,四,两行的代码了,表示引用自己创建的东西,重点是res-auto,这个,一般导入第三方包时,我们都用这个,然后cutomer表示名字,最后引用。
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Tobbar);
        leftTextColor = ta.getColor(R.styleable.Tobbar_leftTextcolor, 0);
        leftparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
        addView(leftbutton, leftparams);
 
 
 
 
 
 
学习日志:学习了解当前的内容是利用视屏的,花了早上和晚上各一小时,再在android上运行验证花了一小时,最后写博客复习花了一小时。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值