求解救 在AndroidStudio 种的自定义属性命名空间问题

micaddress 2016-05-21 10:05:08
开发环境:windows8.1 64bit AndroidStudio 2.0
我定义了一个属性文件 attrs.xml 位于res/values 目录下
内容是
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SettingItemView">
<attr name="title_value" format="string" />
<attr name="desc_on" format="string" />
<attr name="desc_off" format="string" />
</declare-styleable>
</resources>

然后在使用的时候
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:michael="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.michael.administrator.myapplication.activity.SettingActivity">

<com.michael.administrator.myapplication.view.SettingItemView
android:id="@+id/siv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
michael:title_value="title_value"
michael:desc_on="desc_on"
michael:desc_off="desc_off"
/>

以michael 起头的3个标签的内容都获取不到,后台打印均为空
如果我将 上面内容的
xmlns:michael="http://schemas.android.com/apk/res-auto"
改为我的包名
xmlns:michael="com.michael.administrator.myapplication"
app则可以获取到 我这三个自定义标签的内容,后台也能打印出来.但是无法发布,发布的话会提示 命名空间要以 http:// 开头
然后写成 xmlns:michael="http://com.michael.administrator.myapplication"
效果又跟原来一样 获取不到自定义标签内容
求大牛帮忙
...全文
489 4 打赏 收藏 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
初心勿忘 2016-06-24
  • 打赏
  • 举报
回复
写错了,应该是xky:text和xky:testAttr
初心勿忘 2016-06-24
  • 打赏
  • 举报
回复
你好,我也遇到了跟你同样的问题, <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="test"> <attr name="text" format="string" /> <attr name="testAttr" format="integer" /> </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:xkytext="hello" xky:xkytestAttr="3" /> </RelativeLayout> 结果,获取到的值都是空的,这是为什么呢
xkz_5413 2016-05-22
  • 打赏
  • 举报
回复
命名空间里设置成 xmlns:michael="http://schemas.android.com/apk/res-auto" 你获取的时候是自在定义控件里获取属性,通过attrs.get...来获取 这个方法不是需要一个命名空间吗, NAMESPACE = xmlns:michael="http://schemas.android.com/apk/res-auto" attrs.getAttributeValue(NAMESPACE, desc_on); 这样就可以获取啊
micaddress 2016-05-22
  • 打赏
  • 举报
回复
引用 1 楼 xkz_5413 的回复:
命名空间里设置成 xmlns:michael="http://schemas.android.com/apk/res-auto" 你获取的时候是自在定义控件里获取属性,通过attrs.get...来获取 这个方法不是需要一个命名空间吗, NAMESPACE = xmlns:michael="http://schemas.android.com/apk/res-auto" attrs.getAttributeValue(NAMESPACE, desc_on); 这样就可以获取啊
非常感谢 就是这个问题

80,490

社区成员

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

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