Android条目个人中心设置条目可点击条目

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/u010838785/article/details/90071285
样式

在这里插入图片描述

属性文件attr.xml文件添加
<declare-styleable name="ItemView">
    <attr name="text" format="string" />
    <attr name="textColor" format="color" />
    <attr name="icon" format="reference" />
    <attr name="arrow" format="reference" />
    <attr name="hiddenIcon" format="boolean"/>
</declare-styleable>
代码
/**
 * 作者:guoyzh
 * 时间:2018/11/26 13:14
 * 功能:自定义条目展示
 */

public class ItemView extends LinearLayout {

    private ImageView mIcon;
    private TextView mTitle;
    private ImageView mArrow;

    public ItemView(Context context) {
        this(context, null);
    }

    public ItemView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
        // 获取从xml文件中设置的属性
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ItemView, defStyleAttr, 0);
        try {
            mTitle.setText(array.getString(R.styleable.ItemView_text));
            mTitle.setTextColor(array.getColor(R.styleable.ItemView_textColor, Color.BLACK));
            mIcon.setImageResource(array.getResourceId(R.styleable.ItemView_icon, R.mipmap.ic_launcher));
            mArrow.setImageResource(R.mipmap.ic_list_right_arrow);
            if (array.getBoolean(R.styleable.ItemView_hiddenIcon, false)) {
                mIcon.setVisibility(GONE);
                LayoutParams params = (LayoutParams) mTitle.getLayoutParams();
                params.setMargins(ConvertUtils.dp2px(16), 0, 0, 0);
                mTitle.setLayoutParams(params);
            }
        } finally {
            array.recycle();
        }
    }


    private void init(Context context) {
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        ViewGroup.LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setLayoutParams(params);
        setBackgroundColor(Color.parseColor("#FFFFFF"));

        mIcon = new ImageView(context);
        mTitle = new TextView(context);
        mArrow = new ImageView(context);
        addView(mIcon);
        addView(mTitle);
        addView(mArrow);

        LayoutParams iconParams = (LayoutParams) mIcon.getLayoutParams();
        iconParams.width = ConvertUtils.dp2px(16);
        iconParams.height = ConvertUtils.dp2px(16);
        iconParams.setMargins(ConvertUtils.dp2px(16), 0, 0, 0);
        mIcon.setLayoutParams(iconParams);

        LayoutParams textParams = (LayoutParams) mTitle.getLayoutParams();
        textParams.width = ConvertUtils.dp2px(0);
        textParams.height = ConvertUtils.dp2px(22);
        textParams.weight = 1;
        textParams.setMargins(ConvertUtils.dp2px(6), 0, 0, 0);
        mTitle.setLayoutParams(textParams);
        mTitle.setTextSize(16);

        LayoutParams arrowParams = (LayoutParams) mArrow.getLayoutParams();
        arrowParams.width = ConvertUtils.dp2px(16);
        arrowParams.height = ConvertUtils.dp2px(16);
        arrowParams.setMargins(0, 0, ConvertUtils.dp2px(16), 0);
        mArrow.setLayoutParams(arrowParams);
    }
}
使用
<com.hencego.sgpc.view.ItemView
    android:id="@+id/tv_bank_card"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:gravity="center"
    app:hiddenIcon="true"
    app:icon="@null"
    app:text="银行卡"
    app:textColor="#ff9b9b9b"/>

猜你喜欢

转载自blog.csdn.net/u010838785/article/details/90071285
今日推荐