Android自定义控件之组合控件学习笔记分享

所属分类: 软件编程 / Android 阅读数: 1596
收藏 0 赞 0 分享

我们来讲一下自定义组合控件,相信大家也接触过自定义组合控件吧,话不多说,直接干(哈~哈~):

这里写图片描述

大家看到这个觉得这不是很简单的吗,这不就是写个布局文件就搞定嘛,没错,确实直接上布局就行,不过,我只是用这个简单的例子来讲一下自定义组合控件的用法。

首先看看,这一行行的条目看起来都长得差不多,只是图片和文字不一样,没错,就是看中这一点,我们可以把一个条目做成一个组合控件,做为一个整体,这样不管你有几个条目,就写几个组合控件就行了。

步骤:

1.先建立组合控件的布局

myView.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="60dp" >

 <ImageView
  android:id="@+id/icon_Iv"
  android:layout_width="35dp"
  android:layout_height="35dp"
  android:layout_centerVertical="true"
  android:layout_marginLeft="30dp"
  android:src="@drawable/phone_qiyi_explore_friends" />

 <TextView
  android:id="@+id/tv"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:layout_marginLeft="80dp"
  android:gravity="center"
  android:text="朋友圈"
  android:textSize="15sp"
  android:textStyle="bold" />

 <ImageView
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:layout_alignParentRight="true"
  android:layout_marginRight="20dp"
  android:src="@drawable/phone_my_inc_arrow" />

 <View
  android:layout_width="match_parent"
  android:layout_height="0.5dp"
  android:layout_alignParentBottom="true"
  android:layout_marginLeft="5dp"
  android:layout_marginRight="5dp"
  android:background="#000" />

</RelativeLayout>

这里写图片描述

2.自定义属性(图片资源和文本)

在values/目录下新建attrs.xml文件

attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <!-- 自定义属性:src和text -->
 <declare-styleable name="myView_attrs">
  <attr name="src" format="reference"></attr>
  <attr name="text" format="string"></attr>
 </declare-styleable>
</resources>

这里写图片描述

3.新建一个类MyView继承RelativeLayout,将自定义的布局文件加载进来并且获取自定义的属性,然后取得自定义属性字段的值,最后将相应的值设置在相应的组件上

/**
 * 自定义组合控件(包括一个ImageView和TextView)
 * @author Administrator
 *
 */
public class MyView extends RelativeLayout{

 private TextView tv;
 private ImageView icon_Iv;
 public MyView(Context context) {
  this(context,null);
 }
 public MyView(Context context, AttributeSet attrs) {
  super(context, attrs);
  initView(context);
  //拿到自定义的属性
  TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myView_attrs);
  //获取自定义属性的值
  String text = ta.getString(R.styleable.myView_attrs_text);
  Drawable drawable = ta.getDrawable(R.styleable.myView_attrs_src);
  //把值设置到相应组件上
  icon_Iv.setImageDrawable(drawable);
  tv.setText(text);
 }
 private void initView(Context context) {
  //把自定义的布局加载进来
  View.inflate(context,R.layout.myview,this);
  //找到布局中的组件
  icon_Iv = (ImageView) this.findViewById(R.id.icon_Iv);
  tv = (TextView) this.findViewById(R.id.tv);

 }
}

4.在main.xml文件中添加自定义组合控件

注:记得加上命名空间
有几个条目就加几个控件
main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android xmlns:briup="http://schemas.android.com/apk/res/com.example.test"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.example.test.MainActivity" >
 <com.example.test.MyView
  android:id="@+id/myView"
  briup:src="@drawable/phone_qiyi_explore_friends"
  briup:text="朋友圈" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 <com.example.test.MyView
  android:id="@+id/myView1"
  briup:src="@drawable/phone_qiyi_gusslike_icon"
  briup:text="啪啪奇" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 <com.example.test.MyView
  android:id="@+id/myView2"
  briup:text="消息" 
  briup:src="@drawable/phone_qiyi_message_icon"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
</LinearLayout>

这里写图片描述

注:

这里写图片描述

做到以上步骤就可以了,希望本文所述对大家学习Android自定义控件有所帮助。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多