自定义组合控件
来源:互联网 发布:php高并发redis 编辑:程序博客网 时间:2024/06/14 11:05
java代码如下
2:attras.xml文件下
3:主布局XML文件
/** * Created by Administrator on 2017/9/1. * 1、要让组合自定义控件 引入一个布局 并且挂在自身上面 * 2、自定义属性 * 在values 创建一个attrs.xml文件 * 使用<declare-styleable> 写自定义属性 * 在布局文件里面 添加命名空间 * 3、让自定义属性 在控件上显示出效果 * 使用TypedArray 把指定的自定义属性获取出来 (在两个参数的构造方法中,AttributeSet 对象) * 然后把属性值设置在对应的控件上 */public class MyGroupView extends LinearLayout{ View view; ImageView iv; TextView tvTitle,tvDesc; public void init(Context context){ //因为现在使用的是组合自定义控件 那么我们需要一个布局把控件组合在一起 //参数3:是指当前使用布局转换过来的view对象 要挂在的父布局 view = View.inflate(context, R.layout.view_group_view, this); iv = (ImageView)view.findViewById(R.id.view_iv); tvTitle = (TextView)view.findViewById(R.id.view_tv_title); tvDesc = (TextView)view.findViewById(R.id.view_tv_desc); } public void setTitle(String title){ tvTitle.setText(title); } public void setDesc(String desc){ tvDesc.setText(desc); } public void setImgRes(int id){ iv.setImageResource(id); } public MyGroupView(Context context) { this(context, null); } public MyGroupView(Context context, AttributeSet attrs) { this(context, attrs, 0); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyGroupView); setView(typedArray); } private void setView(TypedArray typedArray) { int id = typedArray.getResourceId(R.styleable.MyGroupView_imgRes, R.mipmap.ic_launcher); iv.setImageResource(id); String title = typedArray.getString(R.styleable.MyGroupView_a); tvTitle.setText(title); String desc = typedArray.getString(R.styleable.MyGroupView_b); tvDesc.setText(desc); } public MyGroupView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); }}
2:attras.xml文件下
<?xml version="1.0" encoding="utf-8"?><resources><!-- 自定义的属性--><declare-styleable name="MyImageButton"><attr name="imageResouce" format="reference"/><attr name="text" format="string"/></declare-styleable><declare-styleable name="MyGroupView"><attr name="a" format="string"/><attr name="b" format="string"/><attr name="imgRes" format="reference"/></declare-styleable></resources>
3:主布局XML文件
<com.example.myview.view.MyGroupView android:id="@+id/mgv" android:layout_width="match_parent" android:layout_height="wrap_content" jlj:a="我的标题dfsdf" jlj:b="我的描述12123sdfsdf" jlj:imgRes="@mipmap/a" ></com.example.myview.view.MyGroupView>4:view_group_view Xml文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"><ImageView android:id="@+id/view_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:layout_alignParentLeft="true" android:layout_centerVertical="true" /><TextView android:id="@+id/view_tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/view_iv" android:padding="5dp" android:text="标题" /><TextView android:id="@+id/view_tv_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/view_iv" android:layout_below="@+id/view_tv_title" android:padding="5dp" android:text="描述" /></RelativeLayout>
阅读全文
0 0
- 自定义控件-组合控件
- 自定义组合控件,组合模式
- Android自定义组合控件
- android自定义组合控件
- 自定义组合控件
- 1.11 自定义组合控件
- 自定义组合控件
- 自定义组合控件
- 自定义组合控件
- 自定义组合控件.txt
- Android自定义组合控件
- 07自定义组合控件
- 自定义组合控件
- android组合自定义控件
- android 自定义组合控件
- 自定义组合控件
- android 自定义组合控件
- android 自定义组合控件
- POJ3450 Corporate Identity(kmp,最长公共子串)
- SlidingMenu 侧滑菜单
- AndroidQQ第三方登陆
- java8,你应该了解的新特性(新时间与日期API)
- 清除缓存
- 自定义组合控件
- 自定义控件
- tablayout用法
- Java语言基础学习笔记(四)
- 实现夜间模式
- 初学acmer--c++的泛型库(一)向量
- recyclerview头布局独占一行
- 最简单的解释 webdriver的工作原理
- leetcode 283. Move Zeroes(C语言)