自定义控件属性获取不了的问题

初心勿忘 2016-06-24 03:50:57
如题所示,自定义控件的属性代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="test">
<attr name="text" format="string" />
<attr name="testAttr" format="string" />
</declare-styleable>

</resources>

使用过程中,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xky="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<com.example.administrator.atterbutesettest.MyTextView
android:id="@+id/myView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xky:text="hello"
/>
</RelativeLayout>

然后,
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.test);

String text = ta.getString(R.styleable.test_testAttr);

Log.e(TAG, "text = " + text );
打印的信息却为空,请问为什么呢
...全文
199 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
初心勿忘 2016-06-24
  • 打赏
  • 举报
回复
引用 2 楼 dalor 的回复:
[quote=引用 1 楼 dalor 的回复:] xmlns:xky="http://schemas.android.com/apk/res-auto" 把res-auto改成你的package看看。
res/包名[/quote] 解决了,应该是自定义属性的名称应该和类名一致,参考http://blog.csdn.net/guodongandroid/article/details/51536091
初心勿忘 2016-06-24
  • 打赏
  • 举报
回复
引用 2 楼 dalor 的回复:
[quote=引用 1 楼 dalor 的回复:] xmlns:xky="http://schemas.android.com/apk/res-auto" 把res-auto改成你的package看看。
res/包名[/quote] 不行,会报错, 我用的是android studio
开发者_android 2016-06-24
  • 打赏
  • 举报
回复
引用 1 楼 dalor 的回复:
xmlns:xky="http://schemas.android.com/apk/res-auto" 把res-auto改成你的package看看。
res/包名
开发者_android 2016-06-24
  • 打赏
  • 举报
回复
xmlns:xky="http://schemas.android.com/apk/res-auto" 把res-auto改成你的package看看。

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧