首页 技术 正文
技术 2022年11月7日
0 收藏 926 点赞 844 浏览 4614 个字

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

(一)自定义属性

  在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. 不能忘了自定义命名空间的
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849