Android xml中应用程序名称空间的用途是什么

编程入门 行业动态 更新时间:2024-10-26 16:21:47
本文介绍了Android xml中应用程序名称空间的用途是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面是活动中显示的菜单代码(Sunshine udacity android课程的DetailFragment.xml)

Below is the code of a menu being displayed in an activity(DetailFragment.xml of Sunshine udacity android course)

我不明白为什么下面需要两个不同的名称空间.为什么我不能使用命名空间android:而不是app:

I could not understand why two different namespaces are needed below. Why cant I use the namespace android: instead of app:

当我替换下面的xml部分时 app:actionProviderClass ="android.support.v7.widget.ShareActionProvider" 和 app:actionProviderClass ="android.widget.ShareActionProvider"

In below xml part when I replace app:actionProviderClass="android.support.v7.widget.ShareActionProvider" with app:actionProviderClass="android.widget.ShareActionProvider"

似乎给了一些赋值错误,但是如果将应用程序更改为android,如下所示,效果很好 android:actionProviderClass ="android.widget.ShareActionProvider"

It seemed to give some assignment error, but works fine if app is changed to android as below android:actionProviderClass="android.widget.ShareActionProvider"

我不明白这里到底发生了什么.

I am not able to understand what exactly is happening here.

<menu xmlns:android="schemas.android/apk/res/android" xmlns:app="schemas.android/apk/res-auto"> <item android:id="@+id/action_share" android:title="@string/action_share" app:showAsAction="always" app:actionProviderClass="android.support.v7.widget.ShareActionProvider" /> </menu>

推荐答案

ShareActionProvider有两个版本, Android框架和 v7支持库的.

There're two versions of ShareActionProvider, the android framework's and the v7 support library's.

在Sunshine中,您需要支持最小的SDK版本10,而从API级别14开始将ShareActionProvider添加到框架中,那么如何为SDK 10-13提供功能呢?您可以使用支持库版本.

In Sunshine you need to support min SDK version 10, while ShareActionProvider was added into the framework from API level 14, so how to provide the feature to SDK 10-13? You use the support library version instead.

您在此处将build.gradle中的支持库导入

You import the support library in build.gradle here

dependencies { ... compile 'com.android.support:appcompat-v7:21.0.2' }

现在回到您有关应用程序名称空间的问题. Sunshine正在使用他们支持的(actionProviderClass和showAsAction)的较低版本SDK的框架中不可用的属性,因此他们需要使用支持库提供的自定义属性,并使用自定义属性,您需要使用应用程序命名空间. android名称空间是用于框架属性的,如建议的名称一样.

Now back to your question about the app namespace. Sunshine is using the attributes that are not available in the framework on the lower SDKs they support (actionProviderClass and showAsAction), so they need to use the custom attributes provided by the support library, and to use the custom attributes you need to use the app namespace. The android namespace is for the framework attributes as the name suggested.

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="schemas.android/apk/res/android" xmlns:app="schemas.android/apk/res-auto"> <item android:id="@+id/action_share" android:title="@string/action_share" app:showAsAction="always" app:actionProviderClass="android.support.v7.widget.ShareActionProvider" /> </menu>

更多推荐

Android xml中应用程序名称空间的用途是什么

本文发布于:2023-11-05 03:59:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559832.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   用途   名称   空间   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!