Android xml中应用程序名称空间的用途是什么 [英] Whats the use of app namespace in android xml

查看:99
本文介绍了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="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/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="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/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中应用程序名称空间的用途是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆