好纠结,弄了一个下午老是报错如是总结一下安卓自定视图和自定义属性。

(一)自定义属性

  在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>

总结:

  1. 记得自定义调用构造函数的区别
  2. 不能忘了自定义命名空间的

Android 自定义 attr的更多相关文章

  1. 深入理解Android 自定义attr Style styleable以及其应用

    相信每一位从事Android开发的猿都遇到过需要自己去自定义View的需求,如果想通过xml指定一些我们自己需要的参数,就需要自己声明一个styleable,并在里面自己定义一些attr属性,这个过程 ...

  2. Android 自定义view(二) —— attr 使用

    前言: attr 在前一篇文章<Android 自定义view -- attr理解>已经简单的进行了介绍和创建,那么这篇文章就来一步步说说attr的简单使用吧 自定义view简单实现步骤 ...

  3. Android 自定义ToolBar详细使用

    自定义xml设置ToolBar,通过menu文件扩展选项,通过继承baseactivity使用 1.ToolBar布局 <?xml version="1.0" encodin ...

  4. Android自定义View4——统计图View

    1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...

  5. (转)[原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

  6. (转)android自定义组合控件

    原文地址:http://mypyg.iteye.com/blog/968646 目标:实现textview和ImageButton组合,可以通过Xml设置自定义控件的属性. 1.控件布局:以Linea ...

  7. Android 自定义 view(三)—— onDraw 方法理解

    前言: 上一篇已经介绍了用自己定义的属性怎么简单定义一个view<Android 自定义view(二) -- attr 使用>,那么接下来我们继续深究自定义view,下一步将要去简单理解自 ...

  8. [原] Android 自定义View步骤

    例子如下:Android 自定义View 密码框 例子 1 良好的自定义View 易用,标准,开放. 一个设计良好的自定义view和其他设计良好的类很像.封装了某个具有易用性接口的功能组合,这些功能能 ...

  9. [原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

随机推荐

  1. 微软发布正式版SQL Server 2016

    微软于今天在SQL 官方博客上宣布 SQL Server 数据库软件的正式发布版本(GA),历时一年多,微软为该软件发布了多个公共预览版和候选版本,而今天最终版本终于上线了.在博客中,微软数据集团的企 ...

  2. 07.LoT.UI 前后台通用框架分解系列之——强大的文本编辑器

    LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...

  3. iOS逆向工程之Hopper+LLDB调试第三方App

    LLDB是Low Level Debugger的简称,在iOS开发的调试中LLDB是经常使用的,LLDB是Xcode内置的动态调试工具.使用LLDB可以动态的调试你的应用程序,如果你不做其他的额外处理 ...

  4. nodejs项目mysql使用sequelize支持存储emoji

    nodejs项目mysql使用sequelize支持存储emoji 本篇主要记录nodejs项目阿里云mysql如何支持存储emoji表情. 因由 最近项目遇到用户在文本输入emoji进行存储的时候导 ...

  5. windows环境redis主从安装部署

    准备工作 下载windows环境redis,我下载的是2.4.5,解压,拷贝一主(master)两从(slaveof).主机端口使用6379,两从的端口分别为6380和6381, 我本地索性用6379 ...

  6. ThinkPHP+Smarty模板中截取包含中英文混合的字符串乱码的解决方案

    好几天没写博客了,其实有好多需要总结的,因为最近一直在忙着做项目,但是困惑了几天的Smarty模板中截取包含中英文混合的字符串乱码的问题,终于解决了,所以记录下来,需要的朋友看一下: 出现乱码的原因: ...

  7. PHP代码优化

    1 代码优化 1 尽量静态化 如果一个方法能被静态,那就声明它为静态的,速度可提高1/4,甚至我测试的时候,这个提高了近三倍. 当然了,这个测试方法需要在十万级以上次执行,效果才明显. 其实静态方法和 ...

  8. Mysql基础代码(不断完善中)

    Mysql基础代码,不断完善中~ /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限 ...

  9. 【干货分享】流程DEMO-离职流程

    流程名: 离职申请   流程相关文件: 流程包.xml WebService业务服务.xml WebService.asmx WebService.cs   流程说明: 流程中集成了webservic ...

  10. RMS Server打开或关闭日志记录

    原文: https://technet.microsoft.com/zh-cn/library/cc732758 在 Active Directory Rights Management Servic ...