/** * 自定义类 **/ public class NXCustomMyItemLayout extends LinearLayout { private ImageView mImgLeft; private TextView mtvRight; private TextView mtvLeft; private ImageView mImgRight; private LinearLayout mLlCustomcontrol; private View mViewUnderline; private boolean mIsExpand; //核心方法,两个构造方法和三个构造方法都起到了点缀的意义,最终的最终都要执行到一个参数的方法中 public NXCustomMyItemLayout(Context context) { this(context, null); } public NXCustomMyItemLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public NXCustomMyItemLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs); } private void init(AttributeSet attrs) { LayoutInflater.from(getContext()).inflate(R.layout.customcontrol, this, true); mLlCustomcontrol = (LinearLayout) findViewById(R.id.ll_customcontrol);//条目 mImgLeft = (ImageView) findViewById(R.id.img_left);//左边图片 mtvLeft = (TextView) findViewById(R.id.tv_left);//左边文字 mtvRight = (TextView) findViewById(R.id.tv_right);//右边文字 mImgRight = (ImageView) findViewById(R.id.img_right);//右边图片 mViewUnderline = findViewById(R.id.view_underline);//条目下滑线 TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.MyEntry); if (attributes != null) { //条目的背景颜色 int llCustomControl = attributes.getResourceId(R.styleable.MyEntry_llCustomControl, 0); mLlCustomcontrol.setBackgroundResource(llCustomControl); /** * 处理左边的图片 */ //左边要显示的图片 int imgLeftBackground = attributes.getResourceId(R.styleable.MyEntry_imgLeftBackground, 0); mImgLeft.setImageResource(imgLeftBackground); //获取是否要显示左边的图片 boolean leftImgVisible = attributes.getBoolean(R.styleable.MyEntry_img_left_visible, true); if (leftImgVisible) { mImgLeft.setVisibility(VISIBLE); } else { mImgLeft.setVisibility(GONE); } /** * 处理左边的文字 */ //左边文字 String mTvLeft = attributes.getString(R.styleable.MyEntry_tv_left); if (!TextUtils.isEmpty(mTvLeft)) { mtvLeft.setText(mTvLeft); } //左边文字颜色 int color = attributes.getColor(R.styleable.MyEntry_tv_left_color, 0); mtvLeft.setTextColor(color); //左边文字大小 float size = attributes.getDimension(R.styleable.MyEntry_tv_left_size, 0); mtvLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); /** * 处理右边的图片 */ //处理右边文字 String mTvRight = attributes.getString(R.styleable.MyEntry_tv_right); if (!TextUtils.isEmpty(mTvRight)) { mtvRight.setText(mTvRight); } //右边文字颜色 int righttextcolor = attributes.getColor(R.styleable.MyEntry_tv_right_color, 0); mtvRight.setTextColor(righttextcolor); //右边文字大小 float righttextsize = attributes.getDimension(R.styleable.MyEntry_tv_right_size, 0); mtvRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, righttextsize); /** * 处理右边的图片 */ //右边要显示的图片 int imgRightBackground = attributes.getResourceId(R.styleable.MyEntry_imgRightBackground, 0); mImgRight.setImageResource(imgRightBackground); //获取是否要显示右边的图片 boolean rightImgVisible = attributes.getBoolean(R.styleable.MyEntry_img_right_visible, true); if (rightImgVisible) { mImgRight.setVisibility(VISIBLE); } else { mImgRight.setVisibility(GONE); } //分割线 int viewUnderline = attributes.getColor(R.styleable.MyEntry_viewUnderline, 0); mViewUnderline.setBackgroundColor(viewUnderline); //释放资源 attributes.recycle(); } } /** * 左边图片设置 * 1、图片资源 * 2、是否显示 * * @param resLeftId */ //设置左边图片资源 public void setLeftImageResource(int resLeftId) { mImgLeft.setImageResource(resLeftId); } //设置左边的图片是否显示 public void setImgLeftVisible(int view) { mImgLeft.setVisibility(view); } /** * 左边文字设置 * 1、左边文字 * 2、字体颜色 * 3、字体大小 * 4、是否显示 * * @param text */ //设置左边显示的文字 public void setLeftTextViewText(String text) { mtvLeft.setText(text); } //设置左边字体颜色 public void setLeftTextColor(int textColor) { mtvLeft.setTextColor(textColor); } //设置左边字体大小 public void setLeftTextSize(int textSize) { mtvLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); } //设置左边的文字是否显示 public void setLeftTextVisible(int view) { mtvLeft.setVisibility(view); } /** * 右边文字显示 * 1、右边文字 * 2、字体颜色 * 3、字体大小 * 4、右边文字是否显示 * * @param righttext */ //设置右边显示的文字 public void setRightTextViewText(String righttext) { mtvRight.setText(righttext); } //设置右边显示的文字 public String getRightTextViewText() { return mtvRight.getText().toString(); } //设置右边字体颜色 public void setRightTextColor(int righttextColor) { mtvRight.setTextColor(righttextColor); } //设置右边字体大小 public void setRightTextSize(int righttextSize) { mtvRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, righttextSize); } //设置右边文字是否显示 public void setRightTextVisible(int view) { mtvRight.setVisibility(view); } /** * 右边图片设置 * 1、图片资源 * 2、是否显示 * * @param resRightId */ //设置右边图片资源 public void setRightImageResource(int resRightId) { mImgRight.setImageResource(resRightId); } //设置右边图片资源是否显示 public void setRightImageVisible(int view) { mImgRight.setVisibility(view); } //设置图片的大小 public void setmImgRightSize(int width, int height) { LayoutParams lp = (LayoutParams) mImgRight.getLayoutParams(); lp.width = width; lp.height = height; mImgRight.setLayoutParams(lp); } /** * 设置下划线的颜色 * * @param underline */ public void setView(int underline) { mViewUnderline.setBackgroundColor(underline); } //条目右边按钮处于关闭(箭头向左)状态 public void collapse() { if (mIsExpand) { mIsExpand = false; } } //条目右边按钮处于打开(箭头向下)状态 public void expand() { if (!mIsExpand) { mIsExpand = true; } } //判断条目是打开还是关闭状态 public boolean isExpand() { return mIsExpand; } }
/**
* XML
**/
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="60dp" android:orientation="vertical"> <LinearLayout android:id="@+id/ll_customcontrol" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:id="@+id/img_left" android:layout_width="21dp" android:layout_height="21dp" android:layout_gravity="center" android:layout_marginLeft="15dp" /> <TextView android:id="@+id/tv_left" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="15dp" android:ellipsize="end" android:maxLines="1" tools:text="文本叙述" android:gravity="center_vertical" /> <TextView android:id="@+id/tv_right" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="10dp" android:layout_weight="1" android:ellipsize="end" android:layout_marginLeft="15dp" android:gravity="center_vertical|right" android:maxLines="1" tools:text="文本叙述" /> <ImageView android:id="@+id/img_right" android:layout_width="8dp" android:layout_height="14dp" android:layout_gravity="center" android:layout_marginRight="15dp" /> </LinearLayout> <View android:id="@+id/view_underline" android:layout_width="match_parent" android:layout_height="1dp" /> </LinearLayout>
/**
* attrs
**/
<declare-styleable name="MyEntry"> <attr name="llCustomControl" format="reference" /> <attr name="imgLeftBackground" format="reference" /> <attr name="img_left_visible" format="boolean" /> <attr name="tv_left" format="string" /> <attr name="tv_left_color" format="color" /> <attr name="tv_left_size" format="dimension" /> <attr name="tv_right" format="string" /> <attr name="tv_right_color" format="color" /> <attr name="tv_right_size" format="dimension" /> <attr name="img_right_visible" format="boolean" /> <attr name="imgRightBackground" format="reference" /> <attr name="viewUnderline" format="color" /> </declare-styleable>