Android在xml布局中找不到自定义属性的问题

小生在学习Android自定义属性的过程中遇到自己定义的属性找不到的问题,代码如下:

head_diy.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/head_diy_left_img"
        android:layout_width="0dp"
        android:layout_height="20dp"
        android:layout_weight="1"
        android:src="@drawable/back"
        android:textColor="@color/selected" />

    <TextView
        android:id="@+id/head_diy_among_txt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="6"
        android:gravity="center"
        android:text="这是中间"
        android:textColor="@color/mytext"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/head_diy_right_txt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="选择"
        android:textColor="@color/selected"
        android:textSize="18sp" />

</LinearLayout>
继承LinearLayout的自定义类:

HeadDiyView.class:(在com.yangsheng.diyview包中)

public class HeadDiyView extends LinearLayout {
	private ImageView my_diy_imgleft;
	private TextView my_diy_among_text;
	private TextView my_diy_right_text;
	public HeadDiyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		//把我们的xml转为view对象 并定义本HeadDiyView为根节点
		View view = View.inflate(context, R.layout.head_diy, this);
		//通过findviewbyid取得写满了中的每个控件 因为我们已经定义了HeadDiyView为根节点,所以直接findViewById就行
		this.my_diy_imgleft = (ImageView)findViewById(R.id.head_diy_left_img);
		this.my_diy_right_text = (TextView)findViewById(R.id.head_diy_right_txt);
		this.my_diy_among_text = (TextView)findViewById(R.id.head_diy_among_txt);
		//取得我们定义属性的集合
		//取得我们在自定义控件上设置的资源
		TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);
		Drawable imgleft = array.getDrawable(R.styleable.MyView_left_img);
		String among_text = array.getString(R.styleable.MyView_among_text);
		String right_text = array.getString(R.styleable.MyView_right_text);
		
		
		//取得我们设置的资源之后要给HeadDiyView中的控件设置对应的资源
		this.my_diy_imgleft.setImageDrawable(imgleft);
		this.my_diy_among_text.setText(among_text);
		this.my_diy_right_text.setText(right_text);
		//关闭资源
		
		array.recycle();
	}

}

values中的attrs.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView" >
    <attr name="left_img" format="reference"></attr>
    <attr name="among_text" format="string"></attr>
    <attr name="right_text" format="string"></attr>
</declare-styleable>  


<declare-styleable name="MyViewMore" >
    <attr name="image" format="reference"></attr>
    <attr name="text" format="string"></attr>
    <attr name="imageright" format="reference"></attr>
</declare-styleable>  
</resources>
在以上的基础上在layout中使用HeadDiyView类

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
      xmlns:ysdiy="http://schemas.android.com/apk/res/com.yangsheng.diyview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:orientation="vertical" >
    <com.yangsheng.diyview.HeadDiyView
        android:id="@+id/bundle_phone_land_head"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        ysdiy:right_text="注册"
        ysdiy:among_text="绑定手机登陆"
       /><pre name="code" class="html"><span style="white-space:pre">	</span>... ...
</LinearLayout>

 遇到了: 

error: No resource identifier found for attribute 'right_text' in package 'com.yangsheng.diyview'

在找了一些资料之后知道在我们引用包时应该引用我们Activity所在的包。所以换成我的Activity在的包

 xmlns:ysdiy="http://schemas.android.com/apk/res/com.yangsheng.myapp"就可以了。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值