美文网首页viewAndroid开发Android知识
自定义View之组合控件(上)

自定义View之组合控件(上)

作者: JohnyPeng | 来源:发表于2017-01-11 09:30 被阅读114次

前言

本文主要是针对于自定义ViewGroup的总结,并没有涉及到onMeasure、onLayout、onDraw三个重写方法,主要是针对于实现一个在代码中创建布局Layout的过程!

继承ViewGroup

不一定非要继承自ViewGroup类,ViewGroup的子类例如:LinearLayout、RelativeLayout、FrameLayout等也是可以的

public class BottomBarView extends LinearLayout

自定义属性

为了能够让ViewGroup属性能够在xml文件中进行定义,我们就需要去自定义属性,原因很简单,因为Android自己就这么干的。

  • 先定义一个attrs.xml,这个xml并不会自己生成,要自己创建一个
  • 把自定义的类和属性放进去
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="BottomBarView">
        <attr name="item_text_color" format="color"/>
        <attr name="item_text_select_color" format="color"/>
        <attr name="item_text_size" format="dimension"/>
        <attr name="item_image_size" format="dimension"/>
    </declare-styleable>
</resources>

重写父类的构造方法

一般父类会提供4个带参数构造方法,特别注意:当在xml文件中使用自定义View的时候,一定要重写带有AttributeSet参数的方法

public BottomBarView(Context context) {
        super(context);
        initBottomBar();
    }

public BottomBarView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initBottomBar();
    //加载自定义属性
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BottomBarView);
    if (null != typedArray) {
        mTitleSelectColor = typedArray.getColor(R.styleable.BottomBarView_item_text_select_color, DEFAULT_SELECT_COLOR);
        mTitleTextSize = pxToSp((int) typedArray.getDimension(R.styleable.BottomBarView_item_text_size, spToPx(DEFAULT_TITLE_TEXT_SIZE)));
        mImageSize = pxToDp((int) typedArray.getDimension(R.styleable.BottomBarView_item_image_size, dpToPx(DEFAULT_IMAGE_SIZE)));
    }
    typedArray.recycle();
}

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

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

初始化布局

作为一个自定义的ViewGroup除了装逼之外,就是为了一些常用布局达不到的效果,例如,动态添加和删除控件,动画效果或者是为了统一的风格等等。不过就算是这些,也还是需要加载自定义的属性和自定义的布局,自定义属性在上面的代码中已经说明,不再赘述。在例1中设置的是当前ViewGroup的属性,然后控件是动态添加进去的,而在例2中则是通过一个layout文件直接把控件加入进来,这两种方式都可以,看需求喽。。

例1:

public void initBottomBar() {
    Log.d(LOG_TAG, "-->initBottomBar() ");
    setOrientation(LinearLayout.HORIZONTAL);
    setBackgroundColor(DEFAULT_BACKGROUND);
    ViewGroup.LayoutParams bottomLayoutParams =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpToPx(54.0f));
    setLayoutParams(bottomLayoutParams);
    setGravity(Gravity.CENTER);

    mTitleTextColor = getContext().getColorStateList(R.color.menu_text);
    mTitleTextSize = DEFAULT_TITLE_TEXT_SIZE;
    mTitleSelectColor = DEFAULT_SELECT_COLOR;
    mImageSize = DEFAULT_IMAGE_SIZE;

    mShowItemList = new ArrayList<>();
    mHideItemList = new ArrayList<>();
}

例2:

public void initSearchView() {
    mLayoutInflater = LayoutInflater.from(getContext());

    mSearchLayout = mLayoutInflater.inflate(R.layout.custom_search_view, this);
    mSearchButton = (ImageButton) mSearchLayout.findViewById(R.id.search_icon);
    mSearchText = (EditText) mSearchLayout.findViewById(R.id.search_text);
    mClearButton = (ImageButton) findViewById(R.id.btn_clear);


    mSearchResultContainer = new SearchResultContainer(getContext());


    mSearchText.addTextChangedListener(mSearchTextChangeListener);
    mClearButton.setOnClickListener(mClickListener);


}

总结

以上就是一般ViewGroup自定义的内容了,哈,才这么一点? 额,你还想要多少,其他的都是一些设置属性的东西,我们下一节再说。

相关文章

  • 自定义View之组合控件(下)

    前言 在自定义View之组合控件(上)中讲解初步组合自己的想要的控件,这篇将会讲解如何设置属性和设置控件的位置。 ...

  • android自定义View基础

    自定义View基础1.1 分类自定义View的实现方式有以下几种 类型 定义自定义组合控件 多个控件组合成为一个...

  • 自定义View记录

    自定义View只要有三种类型:自绘控件、组合控件、继承控件。 自绘控件 自定义View主要是因为系统的内置View...

  • android2019-01-03

    1.View的绘制流程自定义控件:1、组合控件。这种自定义控件不需要我们自己绘制,而是使用原生控件组合成的新控件。...

  • 每天五道Android面试题,轻松进大厂2018-12-20

    一、View的绘制流程 自定义控件: 1、组合控件。这种自定义控件不需要我们自己绘制,而是使用原生控件组合成的新控...

  • Android中的自定义控件

    Android中的自定义控件大致可以分成三类:自定义组合控件、继承原生控件的自定义控件、继承View自己实现绘制的...

  • 自定义View之组合控件(上)

    前言 本文主要是针对于自定义ViewGroup的总结,并没有涉及到onMeasure、onLayout、onDra...

  • 自定义控件的实现

    自定义控件可以按类型分三种,自绘控件、组合控件和继承控件 自绘控件:View上所展示的所有内容都是我们自己写在on...

  • 自定义View

    自定义View 1、什么是自定义View? 自定义View可分为三类: a、把系统内置的控件组合起来生成...

  • Android自定义控件之自定义组合控件

    Android自定义控件之自定义组合控件 前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原...

网友评论

    本文标题:自定义View之组合控件(上)

    本文链接:https://www.haomeiwen.com/subject/jrnlbttx.html