自定义View---自定义Title InvalidInt:res/drawable xxhdpi-v4/arrow_back.png

来源:互联网 发布:白苹果刷机数据恢复 编辑:程序博客网 时间:2024/05/29 15:00

1.建立一个类,继承RelativeLayout

2.写好布局

3.在res/values/attr.xml 中自定义其属性

5.在class中实例化自定义属性、布局控件


在eclipse中需要将声明自定义view 跟属性的格式分开

studio中是可以一起写的

<resources>
<span style="white-space:pre"></span><!--name 对应的定义属性,format 就是属性值的格式 -->    <attr name="title_text" format="string" />    <attr name="title_leftimg" format="reference" />    <attr name="title_size" format="dimension" />    <attr name="title_color" format="color" /><span style="white-space:pre"></span><!--声明这个 自定义view,name一般用class名,下面的attr标签就是定义的属性 -->    <declare-styleable name="CustomTitleView">        <attr name="title_text" />        <attr name="title_leftimg" />        <attr name="title_size" />        <attr name="title_color" />    </declare-styleable></resources>
下面是从网上copy别人的一些format格式对应的意思

reference   表示引用,参考某一资源ID 
string   表示字符串 
color   表示颜色值 
dimension   表示尺寸值 
boolean   表示布尔值 
integer   表示整型值 
float   表示浮点值 
fraction   表示百分数 
enum   表示枚举值 
flag   表示位运算 


package com.bdyl.view;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;import com.bdy.ui.R;public class CustomTileView extends RelativeLayout implements OnClickListener {private String mTileName;private Drawable mTileImg;private LayoutInflater inflater;private TextView mTitleNameTxt;private ImageView mBackImg;private OnCustomTileListener mTileListener;//自定义接口,实现监听处理public CustomTileView(Context context) {super(context);init(context, null);}public CustomTileView(Context context, AttributeSet attrs) {super(context, attrs);init(context, attrs);}public CustomTileView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs);}public void setTitleClickListener(OnCustomTileListener tileListener) {mTileListener = tileListener;}/** 实例化属性 */private void init(Context context, AttributeSet attrs) {TypedArray a = null;try {a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.CustomTitleView, 0, 0);mTileName = a.getString(R.styleable.CustomTitleView_title_text);mTileImg = a.getDrawable(R.styleable.CustomTitleView_title_leftimg);} finally {a.recycle();// 公用的需要回收}inflater = LayoutInflater.from(context);initView();}/* 将为属性值赋予意思,为需要的控件赋值 */private void initView() {View v = inflater.inflate(R.layout.custom_title_layout, this, true);if (mTileName != null) {// 只有在自定义属性不为空的时候才实例 化控件,mTitleNameTxt = (TextView) v.findViewById(R.id.custom_title_name_txt);mTitleNameTxt.setText(mTileName);}if (mTileImg != null) {mBackImg = (ImageView) v.findViewById(R.id.custom_title_back_img);mBackImg.setImageDrawable(mTileImg);mBackImg.setOnClickListener(this);}}@Overridepublic void onClick(View v) {mTileListener.setTitleBackClick();//实现接口方法}/* * 自定义接口 实现控件监听 */public interface OnCustomTileListener {public void setTitleBackClick();}public void setText(String text) {this.mTileName = text;}public String getText() {return mTileName;}}
在设置图片资源的时候,遇到一个错误,资源id无效,我没有xxhdpi-v4这个资源文件



原因是:我在设置图片资源的时候用的是 int mTitleImg = a.getInt(R.styleable.CustomTitleView_title_leftimg);  imageView.setImageResource(rsID);

正确的是:a.getDrawable(R.styleable...);返回的的Drawable, Imageview.setImageDrawable(a.getDrawable(....));

0 0
原创粉丝点击