第一步: 属性类(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>