Android实现布局控件自定义属性
一、自定义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实现布局控件自定义属性的更多相关文章
- 【Android】7.1 布局控件常用的公共属性
分类:C#.Android.VS2015: 创建日期:2016-02-10 一.简介 Android应用程序中的布局控件都是容器控件,用于控制子元素的排列和放置方式.Android提供的布局控件有: ...
- Android 一个日历控件的实现代码
转载 2017-05-19 作者:Othershe 我要评论 本篇文章主要介绍了Android 一个日历控件的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看 ...
- Android 中常见控件的介绍和使用
1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...
- Android中ListView控件的使用
Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...
- 五、Android学习第四天补充——Android的常用控件(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 五.Android学习第四天补充——Android的常用控件 熟悉常用的A ...
- 注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式
注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式 这个坑,必须要注意呀, 比如在用ListView的时候,如果在List_ ...
- Android其它新控件 (转)
原文出处:http://blog.csdn.net/lavor_zl/article/details/51312715 Android其它新控件是指非Android大版本更新时提出的新控件,也非谷歌I ...
- 【转】Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用
Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用 分类: Android UI ...
- 【转】Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用
Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用 分类: Android UI2015-06-15 16: ...
随机推荐
- SpringBoot的核心注解和配置
一.入口类和SpringBootApplication Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序的入口方法. @ ...
- map::erase陷阱
map::erase函数在不同版本stl中的差异 1. C++98和C++11标准 http://www.cplusplus.com/reference/map/map/erase/ 2. pj st ...
- undefined reference to `__sync_bool_compare_and_swap_4
然后开始glibc的编译工作. 你必须设定march这个参数才行,要不然会出现“undefined reference to `__sync_bool_compare_and_swap_4′.”这个错 ...
- PHP的pm、pm.max_requests、memory_limit
1.php-fpm.conf中的pm pm是来控制php-fpm的工作进程数到底是一次性产生固定不变(static)还是在运行过程中随着需要动态变化(dynamic).众所周知,工作 进程数与服务器性 ...
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) C - Jon Snow and his Favourite Number
地址:http://codeforces.com/contest/768/problem/C 题目: C. Jon Snow and his Favourite Number time limit p ...
- 使用IDEA 创建Servlet 的时候,找不到javax.servlet
使用IDEA 开发工具,创建Servlet 文件的时候,出现了下面的这种错误, 解决步骤如下: 第一步:点击 File 第二步:找到Project Structure,点击,然后按照下图顺序操作,添加 ...
- 分布式系统 SOA与中间件
在分布式系统中,有一个基础的理论 CAP,Consistency一致性 Availability可用性 Partition Tolerance分区容忍性,任何一个系统都不可能同时满足这三个条件(高富帅 ...
- [HNOI2019]校园旅行(构造+生成树+动规)
题目 [HNOI2019]校园旅行 做法 最朴素的做法就是点对扩展\(O(m^2)\) 发现\(n\)比较小,我们是否能从\(n\)下手减少边数呢?是肯定的 单独看一个颜色的联通块,如果是二分图,我们 ...
- JS对象深入剖析
对象概述 Objects are mutable keyed collections. An object is a container of properties, where a propert ...
- 北京电子科技学院(BESTI)实验报告2
北京电子科技学院(BESTI)实验报告2 课程: 信息安全系统设计基础 班级:1452.1453 姓名:(按贡献大小排名)郑凯杰 .周恩德 学号:(按贡献大小排名)20145314 .20145217 ...