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

前言

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

继承ViewGroup

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

public class BottomBarView extends LinearLayout

自定义属性

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

  • 先定义一个attrs.xml,这个xml并不会自己生成,要自己创建一个
  • 把自定义的类和属性放进去


    
        
        
        
        
    

重写父类的构造方法

一般父类会提供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之组合控件(上))