效果图:
功能:
- 支持item宽高定制
- 支持字体大小、颜色、左边距定制
- 支持小红点是否显示
- 支持下一页跳转图标背景、宽高、左右边距定制
使用
<com.astraea.india.ui.usercenter.views.CommonItemView
android:id="@+id/mine_frame_clear_cache"
android:layout_width="match_parent"
android:layout_height="@dimen/y150"
app:textSize="@dimen/y16"
app:imageMarginRight="@dimen/x58"
app:text="@string/mine_clear_cache"
app:textColor="#0D0E15"
app:isShowRedPoint="true"
app:textMargeLeft="@dimen/x58" />
自定义View部分
- 在values/attrs.xml新建declare-styleable
<declare-styleable name="settingItem">
<attr name="itemHeigh" format="dimension"/>//item高度
<attr name="text" format="string"/>//左TextView的内容
<attr name="textSize" format="dimension"/>//左TextView的字体大小
<attr name="textColor" format="color"/>//左TextView的字体颜色
<attr name="textMargeLeft" format="dimension"/>//左TextView的左边距
<attr name="image" format="reference"/>//跳转下一页图标
<attr name="imageHeigh" format="dimension"/>//跳转下一页图标高度
<attr name="imageWidht" format="dimension"/>//跳转下一页图标宽度
<attr name="imageMarginLeft" format="dimension"/>//跳转下一页图标左边距
<attr name="imageMarginRight" format="dimension"/>//跳转下一页图标右边距
<attr name="isShowRedPoint" format="boolean"/>//是否显示小红点
<attr name="textRight" format="dimension"/>//右TextView内容
</declare-styleable>
- 自定义item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:gravity="center_vertical">
<TextView
android:id="@+id/tv_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/horizontal_margin"
android:gravity="center_vertical"
android:text="@string/mine_version"
android:textColor="#0D0E15"
android:textSize="@dimen/y44" />
<ImageView
android:id="@+id/iv_go"
android:layout_width="@dimen/x21"
android:layout_height="@dimen/y35"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical|right"
android:layout_marginRight="@dimen/horizontal_margin"
android:src="@drawable/next_arrow" />
<View
android:id="@+id/red_point"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/x30"
android:layout_toLeftOf="@+id/iv_go"
android:background="@drawable/update_red_point" />
<TextView
android:layout_toLeftOf="@+id/red_point"
android:layout_marginRight="@dimen/y58"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:id="@+id/tv_right"
android:textColor="#415EAD"
android:layout_width="wrap_content"
android:textSize="@dimen/y44"
android:layout_height="wrap_content" />
</RelativeLayout>
- 布局引用 (必须引入自己的命名空间 xmlns:app=“http://schemas.android.com/apk/res-auto”)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_verify"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/common_toolbar_w" />
<com.astraea.india.ui.usercenter.views.CommonItemView
android:id="@+id/info_frame_reset"
android:layout_width="match_parent"
android:layout_height="@dimen/y150"
app:textSize="@dimen/y16"
app:text="@string/reset_pwd"
app:isShowRedPoint="false"
app:textColor="#0D0E15"
app:imageMarginRight="@dimen/x58"
app:textMargeLeft="@dimen/x58"/>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/y30"
android:background="#F6F6F8"
/>
<com.astraea.india.ui.usercenter.views.CommonItemView
android:id="@+id/mine_frame_clear_cache"
android:layout_width="match_parent"
android:layout_height="@dimen/y150"
app:textSize="@dimen/y16"
app:imageMarginRight="@dimen/x58"
app:text="@string/mine_clear_cache"
app:textColor="#0D0E15"
app:isShowRedPoint="true"
app:textMargeLeft="@dimen/x58" />
<com.astraea.india.ui.usercenter.views.CommonItemView
android:id="@+id/mine_frame_feedback"
android:layout_width="match_parent"
android:layout_height="@dimen/y150"
app:textSize="@dimen/y16"
app:text="@string/mine_feedback"
app:textColor="#0D0E15"
app:isShowRedPoint="false"
app:imageMarginRight="@dimen/x58"
app:textMargeLeft="@dimen/x58" />
<com.astraea.india.ui.usercenter.views.CommonItemView
android:id="@+id/mine_frame_version"
android:layout_width="match_parent"
android:layout_height="@dimen/y150"
app:imageMarginRight="@dimen/x58"
app:textSize="@dimen/y16"
app:text="@string/mine_version"
app:textColor="#0D0E15"
app:textMargeLeft="@dimen/x58" />
<TextView
android:id="@+id/info_tv_logout"
android:layout_width="@dimen/x812"
android:layout_height="@dimen/y128"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/y100"
android:layout_marginBottom="@dimen/y150"
android:background="@drawable/bg_logout_btn"
android:gravity="center"
android:text="@string/logout"
android:textColor="#BD10E0"
android:textSize="@dimen/y44"/>
</LinearLayout>
- CommonSettingItem.java
package com.astraea.india.ui.usercenter.views;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.astraea.india.R;
/**
* Created by Administrator on 2018/12/2.
*/
public class CommonItemView extends LinearLayout {
private TextView mLeftText;
private TextView mRightText;
private View mRedPoint;
private ImageView mGO;
private String mText;
private String mTextRight;
private float mTextSize;
private int mTextMargeLeft;
private int imageHeigh;
private int imageWidth;
private int imageLeftMargin;
private int imageRightMargin;
private boolean isShowRedPoint;
private int background;
private View view;
public CommonItemView(Context context) {
super(context);
}
public CommonItemView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
view = LayoutInflater.from(context).inflate(R.layout.common_setting_item, this);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.settingItem);
mText = array.getString(R.styleable.settingItem_text);
mTextRight= array.getString(R.styleable.settingItem_textRight);
mTextSize = array.getDimension(R.styleable.settingItem_textSize, 14);
mTextMargeLeft = array.getDimensionPixelSize(R.styleable.settingItem_textMargeLeft, 0);
isShowRedPoint = array.getBoolean(R.styleable.settingItem_isShowRedPoint, false);
imageHeigh = array.getDimensionPixelSize(R.styleable.settingItem_imageHeigh, 30);
imageWidth = array.getDimensionPixelSize(R.styleable.settingItem_imageWidht, 30);
imageLeftMargin = array.getDimensionPixelSize(R.styleable.settingItem_imageMarginLeft, 10);
imageRightMargin = array.getDimensionPixelSize(R.styleable.settingItem_imageMarginRight, 10);
background = array.getResourceId(R.styleable.settingItem_image, R.drawable.next_arrow);
initView();
}
public CommonItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void initView() {
setTextParams();
setRedPoint();
setImageParams();
}
private void setTextParams() {
mRightText=view.findViewById(R.id.tv_right);
mLeftText = view.findViewById(R.id.tv_left);
mLeftText.setText(mText);
mLeftText.setTextSize(mTextSize);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mLeftText.getLayoutParams();
params.leftMargin=mTextMargeLeft;
mLeftText.setLayoutParams(params);
mRightText.setText(mTextRight);
}
private void setRedPoint() {
mRedPoint = view.findViewById(R.id.red_point);
mRedPoint.setVisibility(isShowRedPoint ? View.VISIBLE : GONE);
}
private void setImageParams() {
mGO = view.findViewById(R.id.iv_go);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mGO.getLayoutParams();
params.height = imageHeigh;
params.width = imageWidth;
params.leftMargin = imageLeftMargin;
params.rightMargin = imageRightMargin;
mGO.setLayoutParams(params);
mGO.setImageResource(background);
}
}
- item内控件获取
View view=findViewById(R.id.控件ID);
TextView mLeftText=view.findViewByID(R.id.子控件id);