android 自定义组件

第一步: 属性类(attrs.xml) 包含在values 文件夹下

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 个人账户界面的自定义列表的属性 -->
    <declare-styleable name="CustomAccountList">
        <attr name="LeftImage" format="reference"></attr>
        <attr name="RightImage" format="reference"></attr>
        <attr name="Title" format="string"></attr>
    </declare-styleable>
</resources>

第二步:自定义的模板类(CustomAccountList .java)

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vipcio.R;

/**
 * 个人账户界面的自定义列表
 * 实现:我的投资,我的银行卡,我的体验金等
 */

public class CustomAccountList extends RelativeLayout {
    private ImageView letfImage;
    private ImageView rightImage;
    private TextView tilte;

    public CustomAccountList(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        View view = LayoutInflater.from(context).inflate(R.layout.custom_accountlist,this,true);
        letfImage = (ImageView) view.findViewById(R.id.leftImage_costomAccountList);
        rightImage = (ImageView) view.findViewById(R.id.rightImage_costomAccountList);
        tilte = (TextView) view.findViewById(R.id.text_costomAccountList);

        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CustomAccountList);
        if(typedArray != null){
            //左侧图标
            int leftImage = typedArray.getResourceId(R.styleable.CustomAccountList_LeftImage, -1);
            if(leftImage>-1){
                letfImage.setImageResource(leftImage);
            }
            //标题文字
            tilte.setText(typedArray.getText(R.styleable.CustomAccountList_Title));
            //右侧图标
            int rightImage = typedArray.getResourceId(R.styleable.CustomAccountList_RightImage, -1);
            if(rightImage>-1){
                this.rightImage.setImageResource(rightImage);
            }
        }
    }
}

第三步:AccountListView.java 的配置文件(custom_accountlist.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:layout_marginLeft="6dp"
    android:paddingLeft="6dp"
    android:orientation="horizontal"
    android:gravity="center_vertical|center"
    android:background="@drawable/custom_account">
    <ImageView
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:id="@+id/leftImage_costomAccountList"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:textSize="18dp"
        android:id="@+id/text_costomAccountList"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|right">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/logo_arrow_right"
            android:id="@+id/rightImage_costomAccountList"/>
    </LinearLayout>
</LinearLayout>

第四步: 使用 (注意引入 xmlns:CustomAccountListView=”http://schemas.android.com/apk/res-auto” )

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:CustomAccountListView="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
        <com.vipcio.customView.CustomAccountList
            android:id="@+id/accountItemBank"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            CustomAccountList:LeftImage="@mipmap/logo_account_bank"
            CustomAccountList:Title = "我的银行卡" />
        <com.vipcio.customView.CustomAccountList
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           CustomAccountList:LeftImage="@mipmap/logo_account_money"
           CustomAccountList:Title = "我的体验金" />          
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值