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

作者:TheMrNice 时间:2022-09-18 01:09:22 

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

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

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

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

步骤:

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>

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

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>

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

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自定义控件有所帮助。

标签:Android,自定义,控件,组合控件
0
投稿

猜你喜欢

  • Android实现文字上下滚动效果

    2023-02-02 07:40:00
  • C#中#define后面只加一个参数的解释

    2022-09-06 07:23:55
  • c#中DataTable转List的2种方法示例

    2022-05-10 20:51:09
  • SpringMVC的执行过程浅析

    2021-05-31 20:51:11
  • SpringCloud如何创建一个服务提供者provider

    2023-08-01 01:56:33
  • Android简单实现一个颜色渐变的ProgressBar的方法

    2023-07-23 00:30:12
  • 在Ubuntu中安装VSCode并配置C/C++开发环境的方法步骤

    2021-07-04 18:19:19
  • 代理模式:JAVA静态代理和动态代理的实例和实现详解

    2023-06-05 02:19:27
  • Java实现的最大匹配分词算法详解

    2021-12-30 00:50:04
  • 在Eclipse中运行Solr 基础知识

    2021-07-06 22:51:04
  • JavaWeb ServletConfig作用及原理分析讲解

    2021-11-09 05:50:54
  • Android实现上下菜单双向滑动

    2023-06-10 02:43:37
  • Android隐藏顶部状态栏所遇到的问题

    2023-08-02 03:39:26
  • 解析spring cloud ouath2中的Eureka

    2023-10-12 04:07:54
  • 图解Java经典算法冒泡排序的原理与实现

    2023-03-14 21:41:23
  • struts2单个文件上传的两种实现方式

    2023-04-23 05:33:30
  • SpringBoot+SpringCache实现两级缓存(Redis+Caffeine)

    2023-10-02 05:20:58
  • Java 执行CMD命令或执行BAT批处理方式

    2022-10-15 03:57:38
  • 通过Class类获取对象(实例讲解)

    2023-05-19 14:12:58
  • Android ViewDragHelper完全解析 自定义ViewGroup神器

    2023-04-19 05:56:19
  • asp之家 软件编程 m.aspxhome.com