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

Android:布局文件中的差异命名空间,第1张

概述我正在为我的应用程序使用Android支持库AppCompat,我尝试将SearchWidget添加到我的视图中.首先,我遇到了一个非常沮丧的错误,即搜索窗口小部件没有出现在屏幕上.这是菜单xml文件:<menuxmlns:android="http://schemas.android.com/apkes/android"xmlns:app="http://schema

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

<menu xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto">    <item        androID:ID="@+ID/action_example"        androID:title="@string/action_example"        app:showAsAction="withText|ifRoom" />    <item androID:ID="@+ID/search_bar"        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)</menu>

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

<menu xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto">    <item        androID:ID="@+ID/action_example"        androID:title="@string/action_example"        app:showAsAction="withText|ifRoom" />    <item androID:ID="@+ID/search_bar"        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)</menu>

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

<menu xmlns:androID="http://schemas.androID.com/apk/res/androID"    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:

http://developer.android.com/guide/topics/ui/actionbar.html

总结

以上是内存溢出为你收集整理的Android:布局文件中的差异命名空间全部内容,希望文章能够帮你解决Android:布局文件中的差异命名空间所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1110398.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存