android自定义view的自定义属性在xml中快捷键显示问题

自定义view流程:

  1. 自定义view继承View;
  2. 自定义View属性;
  3. 重写3个构造方法(后面解释3个);
  4. 重写onDraw方法;
  5. 在values文件夹下建attrs.xml文件
  6. 完成!
下面以一个demo实现:

自定义MyView继承View

package com.hai.test;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
	/**
	 * 框度
	 */
	private int mCircleWidth;
	/**
	 * 第二圈的颜色
	 */
	private int mSecondColor;

	public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		TypedArray ta = context.getTheme().obtainStyledAttributes(attrs,
				R.styleable.MyView, defStyleAttr, 0);
		int num = ta.getIndexCount();
		for (int i = 0; i < num; i++) {

			int id = ta.getIndex(i);
			switch (id) {
			case R.styleable.MyView_circleWidth:
				break;
			case R.styleable.MyView_secondColor:
				break;
			}
		}
	}

	public MyView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public MyView(Context context) {
		this(context, null);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}

}

在values文件夹下建attrs.xml文件(注意属性名称和自定义View中的属性名称)
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <attr name="secondColor" format="color" />
    <attr name="circleWidth" format="dimension" />

    <declare-styleable name="MyView">
        <attr name="secondColor" />
        <attr name="circleWidth" />
    </declare-styleable>

</resources>
在布局文件中引用自定义MyView,给自定义属性赋值时eclipse会自动加上命名空间如下:xmlns:app="http://schemas.android.com/apk/res/com.hai.test"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.hai.test"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.hai.test.MyView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:secondColor="#0f0"
        app:circleWidth="32" />

</LinearLayout>
附上一张快捷键显示自定义属性的图


注意几个问题:
如在xml中引用自定义View,按快捷键不能显示自定义属性时,解决方法如下:
1,检查自定义View的属性名称和attrs.xml中的属性名称 及(declare-styleable name="MyView")
2,clean下项目
3,重启下eclipse
4,在布局文件中擦掉旧的,重新引入自定义View,

通过以上几部就可以解决 android快捷键不能显示自定义属性 的问题了



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值