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); }