Android自定义View

自定义控件也就是自己来编写控件,而非使用Android中提供的控件

在这种方法中,大概的步骤是这样的

1.我们的自定义控件和其他的控件一样,应该写成一个类,而这个类的属性是是有自己来决定的.

2.我们要在res/values目录下建立一个attrs.xml的文件,并在此文件中增加对控件的属性的定义.

3.使用AttributeSet来完成控件类的构造函数,并在构造函数中将自定义控件类中变量与attrs.xml中的属性连接起来.

4.在自定义控件类中使用这些已经连接的属性变量.

5.将自定义的控件类定义到布局用的xml文件中去.

6.在界面中生成此自定义控件类对象,并加以使用.

创建一个atts.xml

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

    //我们自定义的属性
    //declare-styleable是给自定义控件添加自定义属性用的
    //在<resources>标签下使用<declare-styleable name="Topbar">标签来告诉框架它包含的属性就是自定义控件Topbar中的属性

    <declare-styleable name="Topbar">
        //定义属性名字,所应用资源为string
        <attr name="title" format="string"/>

        //dimension尺寸dp
        <attr name="titleTextSize" format="dimension"/>
        <attr name="titleTextColor" 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>

创建Topbar.java
在自定义控件中写属性,最后在控件方法中获取这些值

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by huang on 2016/8/21.
 */

//继承RelativeLayout并重写构造方法
public class Topbar 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;

    //需要自定义属性就需要添加一个参数AttributeSet
    public Topbar(Context context, AttributeSet attrs) {
        super(context, attrs);

        //利用TypedArray可以调用Topbar
        //使用AttributeSet来完成控件类的构造函数,并在构造函数中将自定义控件类中变量与attrs.xml中的属性连接起来.
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.Topbar);

        //从typeArray获取相应值,第二个参数为默认值,如第一个参数在atts.xml中没有定义,返回第二个参数值  
        leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor,0);
        leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
        leftText = ta.getString(R.styleable.Topbar_leftText);

        rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor,0);
        rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
        rightText = ta.getString(R.styleable.Topbar_rightText);

        titletextSize = ta.getDimension(R.styleable.Topbar_titleTextSize,0);
        titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor,0);
        title = ta.getString(R.styleable.Topbar_title);

        //处理完TypedArray赋值完,要记得回收ta,避免浪费也避免缓存出错
        ta.recycle();

        //处理使用控件
        leftButton = new Button(context);
        rightButton = new Button(context);
        tvTitle = new TextView(context);

        leftButton.setTextColor(leftTextColor);
        leftButton.setBackground(leftBackground);
        leftButton.setText(leftText);


        rightButton.setTextColor(rightTextColor);
        rightButton.setBackground(rightBackground);
        rightButton.setText(rightText);

        tvTitle.setTextColor(titleTextColor);
        tvTitle.setTextSize(titletextSize);
        tvTitle.setText(title);
        tvTitle.setGravity(Gravity.CENTER);

        //
        setBackgroundColor(0xFFF59563);

        //
        leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //添加规则
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);

        //把leftParams加入到leftButton
        addView(leftButton,leftParams);

        rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //添加规则
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);

        //把leftParams加入到leftButton
        addView(rightButton,rightParams);

        titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        //添加规则
        titleParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,TRUE);

        //把leftParams加入到leftButton
        addView(tvTitle,titleParams);

    }
}

在布局文件中的写法

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

//布局文件中使用自定义属性的时候,先引用xmlns
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

//直接写包名
    <com.example.huang.title.Topbar
        android:id="@+id/topbar"
        android:layout_width="wrap_content"
        android:layout_height="50dp"

        custom:leftText="BACK"
        custom:leftTextColor="#FFFFFF"

        custom:rightText="MORE"
        custom:rightTextColor="#FFFFFF"

        custom:title="自定标题"
        custom:titleTextColor="#123412"
        custom:titleTextSize="10sp"/>

</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值