完整做一个自定义控件

为什么到晚上脑子才清楚,多希望白天没有这些那些的事找我。


正题···········正题···········正题


想好自己需要做什么样子的控件

1、创建一个继承 View、ViewGroup、或者LinearLayout等等的 一个类MyView.java(这个就是  自定义的控件   我继承的ViewGroup

2、向MyView里添加控件  或者   直接在该类中自己画  (重写onMeasure、onLayout、onDraw);

添加控件 可以用在代码里用addView(找到MyView这个控件,然后myView.addView(button1) ) ;;;;;;;;; 也可以 XML里添加。

3、在MyView.java 文件里  处理添加的控件。

以下是代码。   mMyText是我的自定义控件

CustomActivity.java  

 private void initTextview() {
        String[] customText = new String[]{"我要打电话了了了了","我要","我要打电","我要打电话了了了了是是是是是是是","我要打电话了了了了"};
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        for (int i =0;i<customText.length;i++) {
            TextView tv = new TextView(this);
            tv.setText(customText[i]);
            tv.setTextColor(Color.RED);
            mMyText.addView(tv,lp);
        }
    }


MyText.java
package myView;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.example.administrator.demo1.R;

import static android.R.attr.padding;
import static android.R.attr.width;
import static android.os.Build.VERSION_CODES.M;

/**
 * Created by Administrator on 2017/9/19.
 */

public class MyText extends ViewGroup {

    private int textSize;
    private int textSpace;

    public MyText(Context context) {
        super(context);
    }

    public MyText(Context context, AttributeSet attrs) {
        super(context, attrs);
        //自定义属性
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyText);
        textSize = ta.getDimensionPixelSize(R.styleable.MyText_TextSize, 10);
        textSpace = ta.getDimensionPixelSize(R.styleable.MyText_TextSpace, 5);
        ta.recycle();
    }

    public MyText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取该自定义控件 的 宽高 和 模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();

        int lineWidth =0;
        int lineHeight = 0;
        int height = 0;
        //在获取子控件宽高的时候 一定要先测量
        measureChildren(widthMeasureSpec,heightMeasureSpec);
        //获取子控件数量
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            //获取子控件及其宽高
            View child = getChildAt(i);
            //用这个方法也可以,这个方式是具体测量某一个子控件
            //measureChild(child, widthMeasureSpec, heightMeasureSpec);
            //获取子控件的宽高
            int width = child.getMeasuredWidth();
            height = child.getMeasuredHeight();
            //当余下的宽度不足于显示下一个子控件  要进行换行
            if (lineWidth + width  > widthSize) {
                //超出一行最大宽度  换行
                lineHeight = lineHeight + height+ textSpace;
                lineWidth = paddingLeft+width +textSpace;
                //给child设置tag  供onLayout  设置子控件位置使用
                child.setTag(new Location(paddingLeft, lineHeight, lineWidth, lineHeight + height));
            }else{
                child.setTag(new Location(lineWidth+paddingLeft,lineHeight,lineWidth+paddingLeft+width,lineHeight+height));
                lineWidth = lineWidth+paddingLeft +width+textSpace;
            }
        }

        //如果 是warp_content 注意  这里的参数。  一定要是最下面控件的 的底部的Y值   否则就会出现最下一行的控件不显示
        //同理  width也是一样的。   只适用于 属性为warp_content   此处 width 我用的属性是  match_parent
        setMeasuredDimension(lineWidth,lineHeight+height+textSpace);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            Location mLocation = (Location) child.getTag();

            child.layout(mLocation.left, mLocation.top, mLocation.right, mLocation.bottom);
        }

    }

    private class Location {
        public Location(int left, int top, int right, int bottom) {
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
        }

        public int right;
        public int left;
        public int top;
        public int bottom;
    }


}

自定义属性  放在res/valus/attrs.xml里  没有这个文件就自己创建
<declare-styleable name="TagsLayout">
    <attr name="tagVerticalSpace" format="dimension" />
    <attr name="tagHorizontalSpace" format="dimension" />
</declare-styleable>

在控件里使用

<myView.MyText
android:id="@+id/custom_mytext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:TextSize="15dp"
app:TextSpace="10dp">
 
studio会自动添加
xmlns:app="http://schemas.android.com/apk/res-auto"

eclipse需要自己在布局文件里自己写。否则将不能调用 自定义属性
明天找个更炫酷的自定义控件试手············· 下班喽~~~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值