private void init(Context context, AttributeSet attrs) { mContext = context; TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ExpandableView); mAnimationDuration = typedArray.getInt(R.styleable.ExpandableView_animDuration, DEFAULT_ANIM_DURATION); mAnimAlphaStart = typedArray.getFloat(R.styleable.ExpandableView_animAlphaStart, DEFAULT_ANIM_ALPHA_START); mExpandDrawable = typedArray.getDrawable(R.styleable.ExpandableView_expandDrawable); mCollapseDrawable = typedArray.getDrawable(R.styleable.ExpandableView_collapseDrawable); mCollapsed = typedArray.getBoolean(R.styleable.ExpandableView_viewTitleShow, DEFAULT_SHOW); mTitleImage = typedArray.getDrawable(R.styleable.ExpandableView_viewTitleImage); if (mExpandDrawable == null) { mExpandDrawable = getDrawable(getContext(), R.drawable.ic_expand_more_black_16dp); } if (mCollapseDrawable == null) { mCollapseDrawable = getDrawable(getContext(), R.drawable.ic_expand_less_black_16dp); } setOrientation(LinearLayout.VERTICAL); title = new RelativeLayout(context); int color = typedArray.getColor(R.styleable.ExpandableView_viewTitleBacColor, Color.WHITE); title.setOnClickListener(this); title.setBackgroundColor(color); // title.setGravity(Gravity.CENTER_VERTICAL); title.setVerticalGravity(Gravity.CENTER_VERTICAL); if (mTitleImage != null) { initTitleImage(); } mTitlt = typedArray.getString(R.styleable.ExpandableView_viewTitle); title_size = typedArray.getDimension( R.styleable.ExpandableView_viewTitleSize, DisplayUtil.sp2px(mContext, 10)); title_color = typedArray.getColor( R.styleable.ExpandableView_viewTitleColor, getResources().getColor(R.color.color_909090)); line_color = typedArray.getColor( R.styleable.ExpandableView_viewTitleLineColor, getResources().getColor(R.color.color_bbbbbb)); if (mTitlt != null) { initTitleText(); } typedArray.recycle(); LayoutParams layoutParams = new LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); // 定义一个LayoutParams layoutParams.setMargins(0, DisplayUtil.dip2px(context, 10), 0, 0); setLayoutParams(layoutParams); }
/** 初始化标题题目 */ private void initTitleText() { int padLeft = DisplayUtil.dip2px(mContext, 25); int h = 0; if (mTitleImage != null) { h = mTitleImage.getMinimumHeight(); } TextView tv = new TextView(mContext); tv.setTextSize(title_size); tv.setTextColor(title_color); tv.setText(mTitlt); int padTop = DisplayUtil.dip2px(mContext, 10); tv.setPadding(padLeft + h, padTop, 0, padTop); title.addView(tv); mButton = new ImageButton(mContext); mButton.setImageDrawable(mCollapsed ? mExpandDrawable : mCollapseDrawable); h = mExpandDrawable.getMinimumHeight(); MarginLayoutParams mp = new MarginLayoutParams(h, h); // item的宽高 mp.setMargins(0, 0, padLeft, 0); // 分别是margin_top那四个属性 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mp); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); params.addRule(RelativeLayout.CENTER_VERTICAL); mButton.setLayoutParams(params); title.addView(mButton); // title.measure(0, 0); View line = new View(mContext); line.setBackgroundColor(line_color); h = DisplayUtil.dip2px(mContext, 1); MarginLayoutParams mpLine = new MarginLayoutParams(LayoutParams.MATCH_PARENT, h); // item的宽高 title.measure(0, 0); h = title.getMeasuredHeight() - h; mpLine.setMargins(0, h, 0, 0); // 分别是margin_top那四个属性 RelativeLayout.LayoutParams mpLineparams = new RelativeLayout.LayoutParams(mpLine); line.setLayoutParams(mpLineparams); title.addView(line); View line1 = new View(mContext); line1.setBackgroundColor(line_color); h = DisplayUtil.dip2px(mContext, 1); MarginLayoutParams mpLine1 = new MarginLayoutParams(LayoutParams.MATCH_PARENT, h); // item的宽高 RelativeLayout.LayoutParams mpLineparams1 = new RelativeLayout.LayoutParams(mpLine1); line1.setLayoutParams(mpLineparams1); title.addView(line1); mMinHeight = title.getMeasuredHeight(); addView(title); }
/** 初始化标题图片 */ private void initTitleImage() { int padLeft = DisplayUtil.dip2px(mContext, 20); int h = mTitleImage.getMinimumHeight(); ImageView imageView = new ImageView(mContext); imageView.setImageDrawable(mTitleImage); MarginLayoutParams mp = new MarginLayoutParams(h, h); // item的宽高 mp.setMargins(padLeft, 0, 0, 0); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mp); params.addRule(RelativeLayout.CENTER_VERTICAL); imageView.setLayoutParams(params); title.addView(imageView); }