一、自定义ViewGroup

1、onMeasure

决定内部View(子View)的宽度和高度,以及自己的宽度和高度

2、onLayout

决定子View放置的位置

3、onTouchEvent

定义动作

二、自定义属性

在实际的使用自定义Viewgroup时,经常会用到自定义控件的属性。

在res/values文件夹下建立attr.xml文件

1、书写xml文件,定义<attr>中的自定义属性,在<declare-stableable>中声明已经定义的属性

2、使用自定义的属性,在使用时添加命名空间,xmlns:ren="http://schemas.android.com/apk/res/bupt.ren.slidingmenu",然后即可以使用该属性

注:其中bupt.ren.slidingmenu是应用程序的包名,不是view所在的路径。

三、使用自定义的ViewGroup和自定义布局属性

1、自定义布局属性

attr.xml中定义如下:

<resources>
<attr name="rightPadding" format="dimension"></attr> //定义布局属性
<declare-styleable name="SlidingMenu"> //定义布局,其中name后必须跟着view的类名
<attr name="rightPadding"></attr> //声明属性
</declare-styleable>
</resources>

2、使用自定义布局属性

使用时,在main.xml定义如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ren="http://schemas.android.com/apk/res/bupt.ren.slidingmenu"
android:layout_width="match_parent"
android:layout_height="match_parent" > <bupt.ren.slidingmenu.view.SlidingMenu
android:id="@+id/id_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img_frame_background"
ren:rightPadding="80dp" >
</bupt.ren.slidingmenu.view.SlidingMenu> </RelativeLayout>

3、自定义ViewGroup

在使用自定义属性时,需要覆写ViewGroup中有2个参数的构造方法,然后通过含有2个参数的构造方法,调用含有3个参数的构造方法。含有3个参数的构造方法负责接收自定义属性。

public SlidingMenu(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.SlidingMenu, defStyle, 0); int n = a.getIndexCount();
for (int i = 0; i < n; i++)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.SlidingMenu_rightPadding:
mMenuRightPadding = a.getDimensionPixelSize(attr,
(int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, context
.getResources().getDisplayMetrics()));
break;
}
}
a.recycle(); //需要对TypeArray进行回收
}

获取自定义的属性,其中关键的步骤就是获取mMenuRightPadding这个参数,达到控制控件布局的效果。

得到mMenuRightPadding这个参数后,就可以再ViewGroup中的onMeasure的方法中对子View进行布局的操控。

1.转载注明:http://www.cnblogs.com/yuanblog/p/4439186.html

2.本文为个人笔记、心得,可能引用其它文章,所以博客只在私自范围内供大家学习参考。

Android实现布局控件自定义属性的更多相关文章

  1. 【Android】7.1 布局控件常用的公共属性

    分类:C#.Android.VS2015: 创建日期:2016-02-10 一.简介 Android应用程序中的布局控件都是容器控件,用于控制子元素的排列和放置方式.Android提供的布局控件有: ...

  2. Android 一个日历控件的实现代码

    转载  2017-05-19   作者:Othershe   我要评论 本篇文章主要介绍了Android 一个日历控件的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看 ...

  3. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

  4. Android中ListView控件的使用

    Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...

  5. 五、Android学习第四天补充——Android的常用控件(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 五.Android学习第四天补充——Android的常用控件 熟悉常用的A ...

  6. 注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式

    注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式 这个坑,必须要注意呀, 比如在用ListView的时候,如果在List_ ...

  7. Android其它新控件 (转)

    原文出处:http://blog.csdn.net/lavor_zl/article/details/51312715 Android其它新控件是指非Android大版本更新时提出的新控件,也非谷歌I ...

  8. 【转】Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用

    Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用 分类: Android UI ...

  9. 【转】Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用

    Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用 分类: Android UI2015-06-15 16: ...

随机推荐

  1. 生信-cufflinks输入输出文件分析

    转自:https://wenku.baidu.com/view/8d6a95d20d22590102020740be1e650e52eacf2a.html 输出包含3个文件:转录组的组装.gtf    ...

  2. 编辑器——vscode

    1.编辑器个人工作配置 // 将设置放入此文件中以覆盖默认设置 { "editor.tabSize": 2, "workbench.iconTheme": &q ...

  3. 记一次服务器迁移SVN客户端更换IP

    服务器迁移,SVN服务端IP由原10.58.8.231更换至10.58.1.230   TortoiseSVN更换ip地址操作如下: 打开svn项目的根目录,右键如图操作: 修改ip地址为10.58. ...

  4. [Windows Powershell]-学习笔记(3)

    Powershell 通过函数扩展别名 在powershell中设置别名的确方便快捷,但是在设置别名的过程中并设置参数的相关信息,尽管别名会自动识别参数,但是如何把经常使用的参数默认设定在别名里面呢? ...

  5. fzu1901Period II

    地址:http://acm.fzu.edu.cn/problem.php?pid=1901 题目: Problem 1901 Period II Accept: 442    Submit: 1099 ...

  6. The 15th UESTC Programming Contest Preliminary M - Minimum C0st cdoj1557

    地址:http://acm.uestc.edu.cn/#/problem/show/1557 题目: Minimum C0st Time Limit: 3000/1000MS (Java/Others ...

  7. 2018.9 ECNU ICPC/CCPC Trial Round #2 Query On Tree (树链剖分+线段树维护)

    传送门:https://acm.ecnu.edu.cn/contest/105/problem/Q/ 一棵树,支持两种操作:给一条路径上的节点加上一个等差数列;求两点路径上节点和. 很明显,熟练剖分. ...

  8. [转]linux内核分析笔记----内存管理

    转自:http://blog.csdn.net/Baiduluckyboy/article/details/9667933 内存管理,不用多说,言简意赅.在内核里分配内存还真不是件容易的事情,根本上是 ...

  9. Could not connect to '192.168.89.144' (port 22)

    xshell连接linux主机时,会出现错误:Could not connect to '192.168.89.144' (port 22): Connection failed. 但是这时能ping ...

  10. Greatest Common Increasing Subsequence

    /*HDU1423 最长公共递增*/ #include <stdio.h> #include <string.h> #include <iostream> usin ...