自定义View---设置 item

效果图:
在这里插入图片描述
功能:

  1. 支持item宽高定制
  2. 支持字体大小、颜色、左边距定制
  3. 支持小红点是否显示
  4. 支持下一页跳转图标背景、宽高、左右边距定制

使用

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

Demo地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值