Android 自定义 attr
好纠结,弄了一个下午老是报错如是总结一下安卓自定视图和自定义属性。
(一)自定义属性
在Values文件下建立一个attrs.xml文件,attr的format可以参考:http://www.cnblogs.com/crashmaker/p/3521310.html
<?xml version="1.0" encoding="utf-8"?>
<resources> <declare-styleable name="nfyh">
<attr name="title" format="reference"/>
<attr name="icon" format="reference"/>
<attr name="border_width" format="dimension"/>
<attr name="enable_right" format="boolean"/>
<attr name="selected" format="boolean"/>
</declare-styleable> </resources>
(一)自定义视图(View)
/**
* 列表项视图
*
* @author Chenrui
*
*/
public class ListItemView extends LinearLayout
{ private TextView tvTitle;
private TextView tvSubTitle;
private ImageView imgNext;
private OnClickListener listener;
private Context context;
private View viewBorder;
private LinearLayout customerViews;
private Intent nextIntent; public ListItemView(Context context,AttributeSet attrs)
{
this(context, attrs, 0); // 使用XML文件布局的时候,不会调用 ListItemView(Context context) 的构造函数,如果有主题的需要调用这个构造函数
} public ListItemView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
this.context = context;
LayoutInflater.from(context).inflate(R.layout.view_list_item, this); //初始化布局
this.tvTitle = (TextView) this
.findViewById(R.id.tv_view_liset_item_title);
this.tvSubTitle = (TextView) this
.findViewById(R.id.tv_view_liset_item_sub_title);
this.imgNext = (ImageView) this
.findViewById(R.id.img_view_liset_item_right); this.viewBorder = findViewById(R.id.view_list_item_border);
customerViews = (LinearLayout) this
.findViewById(R.id.viewStub_view_list_item); initView(attrs, defStyle);
} private void initView(AttributeSet attrs, int defStyle)
{
if (attrs == null) return; //赋值 TypedArray typeArray = null;
try
{
typeArray = context.obtainStyledAttributes(attrs, R.styleable.nfyh,
defStyle, 0); //这里获取自定义属性 int count = typeArray.getIndexCount();
//试过使用typeArray.getString(R.styleable.nfyh_title);获取不了值,不知道为何??!!
for (int i = 0; i < count; i++)
{
int index = typeArray.getIndex(i);
switch (index)
{
case R.styleable.nfyh_title:
this.setText(typeArray.getString(index)); break;
case R.styleable.nfyh_enable_right:
this.setNextEnable(typeArray.getBoolean(index, false));
break;
case R.styleable.nfyh_selected:
if (typeArray.getBoolean(index, false)) this
.setSelected();
break;
case R.styleable.nfyh_icon:
this.setIcon(typeArray.getResourceId(index, 0));
break;
case R.styleable.nfyh_border_width:
this.setBorder(typeArray.getDimensionPixelOffset(index,
1));
break;
default:
break;
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
typeArray.recycle();//一定要调用回收
}
} @Override
public void setOnClickListener(View.OnClickListener l)
{
super.setOnClickListener(l);
this.listener = l;
} public void setIcon(int id)
{
this.tvTitle.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0); } public void setText(String text)
{
this.tvTitle.setText(text);
} public void setSubText(String text)
{
this.tvSubTitle.setText(text);
} public void setSelected()
{
this.tvTitle.setTextColor(context.getResources()
.getColor(R.color.green));
} public void setBorder(int height)
{
this.viewBorder.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, height));
// this.viewBorder.setBackgroundColor(colorID);
} public void setNextEnable(boolean value)
{
int visibility = value ? View.VISIBLE : View.GONE;
this.imgNext.setVisibility(visibility);
} public void setCustomerView(View v)
{
customerViews.addView(v);
} public void removeCustomerViews()
{
customerViews.removeAllViews();
} public void setNextIntent(Intent intent)
{
this.nextIntent = intent;
} }
在布局文件中使用自定义视图,直接使用包名。
<com.yixin.nfyh.cloud.ui.ListItemView style="@style/Widget.Light.IOS.ListItem.Title"
nfyh:title="@string/canshuxuanzhe">
</com.yixin.nfyh.cloud.ui.ListItemView>
这里很重要的是命名空间,这里的命名空间(xmlns:nfyh)是可以随便命名的,和attrs的name不必对上。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:nfyh="http://schemas.android.com/apk/res-auto" //注意这里,res-auto也可以是你的包名 xmlns:nfyh="http://schemas.android.com/apk/res/com.yixin.nfyh.cloud"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeded"
android:orientation="vertical" >
<com.yixin.nfyh.cloud.ui.ListItemView style="@style/Widget.Light.IOS.ListItem.Title"
nfyh:title="@string/canshuxuanzhe">
</com.yixin.nfyh.cloud.ui.ListItemView> <LinearLayout
android:id="@+id/ll_ui_sign_detail_params_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
.... 此处省略
style文件
<style name="Widget.Light.IOS.ListItem.Title" parent="@style/match_warp">
<item name="android:layout_marginBottom">10dp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:orientation">vertical</item>
<!-- ...以下自定以属性 -->
<item name="border_width">5dp</item>
<item name="icon">@drawable/ic_tile_list</item>
<item name="selected">true</item>
<item name="enable_right">false</item>
</style>
总结:
- 记得自定义调用构造函数的区别
- 不能忘了自定义命名空间的
Android 自定义 attr的更多相关文章
- 深入理解Android 自定义attr Style styleable以及其应用
相信每一位从事Android开发的猿都遇到过需要自己去自定义View的需求,如果想通过xml指定一些我们自己需要的参数,就需要自己声明一个styleable,并在里面自己定义一些attr属性,这个过程 ...
- Android 自定义view(二) —— attr 使用
前言: attr 在前一篇文章<Android 自定义view -- attr理解>已经简单的进行了介绍和创建,那么这篇文章就来一步步说说attr的简单使用吧 自定义view简单实现步骤 ...
- Android 自定义ToolBar详细使用
自定义xml设置ToolBar,通过menu文件扩展选项,通过继承baseactivity使用 1.ToolBar布局 <?xml version="1.0" encodin ...
- Android自定义View4——统计图View
1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...
- (转)[原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
- (转)android自定义组合控件
原文地址:http://mypyg.iteye.com/blog/968646 目标:实现textview和ImageButton组合,可以通过Xml设置自定义控件的属性. 1.控件布局:以Linea ...
- Android 自定义 view(三)—— onDraw 方法理解
前言: 上一篇已经介绍了用自己定义的属性怎么简单定义一个view<Android 自定义view(二) -- attr 使用>,那么接下来我们继续深究自定义view,下一步将要去简单理解自 ...
- [原] Android 自定义View步骤
例子如下:Android 自定义View 密码框 例子 1 良好的自定义View 易用,标准,开放. 一个设计良好的自定义view和其他设计良好的类很像.封装了某个具有易用性接口的功能组合,这些功能能 ...
- [原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
随机推荐
- python自动化测试(2)-自动化基本技术原理
python自动化测试(2) 自动化基本技术原理 1 概述 在之前的文章里面提到过:做自动化的首要本领就是要会 透过现象看本质 ,落实到实际的IT工作中就是 透过界面看数据. 掌握上面的这样的本领 ...
- ASP.NET Core应用针对静态文件请求的处理[2]: 条件请求与区间请求
通过调用ApplicationBuilder的扩展方法UseStaticFiles注册的StaticFileMiddleware中间件帮助我们处理针对文件的请求.对于StaticFileMiddlew ...
- 11、Struts2 的文件上传和下载
文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- JQuery 选择器
选择器是JQuery的根基,在JQuery中,对事件的处理,遍历DOM和AJAX操作都依赖于选择器.如果能够熟练地使用选择器,不仅能简化代码,而且还可以事半功倍. JQuery选择器的优势 1.简洁的 ...
- [原] KVM 虚拟化原理探究 —— 目录
KVM 虚拟化原理探究 -- 目录 标签(空格分隔): KVM KVM 虚拟化原理探究(1)- overview KVM 虚拟化原理探究(2)- QEMU启动过程 KVM 虚拟化原理探究(3)- CP ...
- C# BackgroundWorker 详解
在C#程序中,经常会有一些耗时较长的CPU密集型运算,如果直接在 UI 线程执行这样的运算就会出现UI不响应的问题.解决这类问题的主要途径是使用多线程,启动一个后台线程,把运算操作放在这个后台线程中完 ...
- BlockingCollection使用
BlockingCollection是一个线程安全的生产者-消费者集合. 代码 public class BlockingTest { BlockingCollection<int> bc ...
- 微服务与Docker介绍
什么是微服务 微服务应用的一个最大的优点是,它们往往比传统的应用程序更有效地利用计算资源.这是因为它们通过扩展组件来处理功能瓶颈问题.这样一来,开发人员只需要为额外的组件部署计算资源,而不需要部署一个 ...
- PHP设计模式(五)建造者模式(Builder For PHP)
建造者模式:将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示的设计模式. 设计场景: 有一个用户的UserInfo类,创建这个类,需要创建用户的姓名,年龄,爱好等信息,才能获得用 ...