android 自定义组件
来源:互联网 发布:乐高ev3机器人编程软件 编辑:程序博客网 时间:2024/05/22 15:24
第一步: 属性类(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>
阅读全文
0 0
- 【Android】Android自定义组件
- 自定义android RadioPreference组件
- android自定义组件
- Android自定义组件
- Android自定义Button组件
- Android-构造自定义组件
- android 自定义view组件
- android 自定义组件
- android自定义组件
- Android-创建自定义组件
- android 绘图、自定义组件
- android自定义分页组件
- Android 自定义分页组件
- Android自定义AlertDialog组件
- Android 自定义分页组件
- android 中自定义组件
- android 自定义组件
- Android自定义组件(1)
- 如何使用文件字节输出流(FileOutputStream)将数据写到磁盘文件中
- c++调试记录--内存无法访问以及逻辑正确却总是得到错误结果
- 过滤器的使用(一)
- MySQL数据类型
- 【LeetCode】1.Two Sum思路以及知识点复习
- android 自定义组件
- div水平垂直居中的方法
- 【51Nod1202】子序列个数
- Python安装第三方库(macOS)
- 流水灯程序
- My eclipse 2017 Stable 1.0 安装破解方法
- Java数组的引用
- 栈、队列、优先队列笔记
- 关于mysql日志的一些参数