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步骤 ...
随机推荐
- Be Better:遇见更好的自己-2016年记
其实并不能找到好的词语来形容过去的一年,感觉就如此平淡的过了!没有了毕业的稚气,看事情淡了,少了一丝浮躁,多了一分认真.2016也许就是那句话-多读书,多看报,少吃零食多睡觉,而我更愿意说--Be B ...
- 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...
- SQL Server on Linux 理由浅析
SQL Server on Linux 理由浅析 今天的爆炸性新闻<SQL Server on Linux>基本上在各大科技媒体上刷屏了 大家看到这个新闻都觉得非常震精,而美股,今天微软开 ...
- wordpress多站点配置
wordpress作为全球第一的个人博客搭建平台一直在国内外有着较高的人气,从3.0版本开始就已经支持多站点的搭建.该功能可以让子站点运行主站点的程序,不需要再每个站点分别存放网站程序.最近更新的4. ...
- Bringing Whoops Back to Laravel 5
You might be missing the "prettier" Whoops error handler from Laravel 4. If so, here's how ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
- Java中Comparable与Comparator的区别
相同 Comparable和Comparator都是用来实现对象的比较.排序 要想对象比较.排序,都需要实现Comparable或Comparator接口 Comparable和Comparator都 ...
- 解读发布:.NET Core RC2 and .NET Core SDK Preview 1
先看一下 .NET Core(包含 ASP.NET Core)的路线图: Beta6: 2015年7月27日 Beta7: 2015年9月2日 Beta8: 2015年10月15日 RC1: 2015 ...
- jsp页面无法识别el表达式的解决方案
今天在写一个springmvc的小demo时,碰到一个问题,在jsp页面中书写为${user.username}的表达式语言,在浏览器页面中仍然显示为${user.username},说明jsp根本不 ...
- CSS3自定义滚动条样式 -webkit-scrollbar(转)
有没有觉得浏览器自带的原始滚动条很不美观,同时也有看到很多网站的自定义滚动条显得高端,就连chrome32.0开发板都抛弃了原始的滚动条,美观多了.那webkit浏览器是如何自定义滚动条的呢? 前言 ...