android中命名空间,Android:布局文件中的差异命名空间

我正在为我的应用程序使用Android支持库AppCompat,我尝试将Search Widget添加到我的视图中.首先,我遇到了一个非常沮丧的错误,即搜索窗口小部件没有出现在屏幕上.这是菜单xml文件:

xmlns:app="http://schemas.android.com/apk/res-auto">

android:id="@+id/action_example"

android:title="@string/action_example"

app:showAsAction="withText|ifRoom" />

android:title="@string/search_title"

android:icon="@drawable/ic_menu_search"

android:showAsAction="always" (LINE ONE)

android:actionViewClass="android.support.v7.widget.SearchView"/> (LINE TWO)

经过几个小时的调试.我注意到错误并更改为以下xml文件:

xmlns:app="http://schemas.android.com/apk/res-auto">

android:id="@+id/action_example"

android:title="@string/action_example"

app:showAsAction="withText|ifRoom" />

android:title="@string/search_title"

android:icon="@drawable/ic_menu_search"

app:showAsAction="always" (LINE ONE)

app:actionViewClass="android.support.v7.widget.SearchView"/> (LINE TWO)

正如你注意到LINE ONE和LINE TWO略有不同,我将android改为app.这神奇地起作用.但是,我不知道这两条线背后有什么区别.我们宣布时有什么区别和意义:

xmlns:app="http://schemas.android.com/apk/res-auto">

我有谷歌,但没有帖子涉及我的问题和问题.

谢谢 :)

解决方法:

你使用自己的自定义命名空间“app”,因为Android框架中的旧版Android版本中不存在属性“showasaction”.因此,当您使用action compat库时,您需要为该属性声明自己的命名空间.

from developers doc

Using XML attributes from the support library

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

If your menu item supplies both a title and an icon—with the title and icon attributes—then the action item shows only the icon by default. If you want to display the text title, add “withText” to the showAsAction attribute. For example:

标签:android,android-layout

来源: https://codeday.me/bug/20190825/1718765.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值