【问题标题】:What is the difference between the name attribute and context attribute in android XML?android XML中的name属性和context属性有什么区别?
【发布时间】:2018-07-27 20:37:23
【问题描述】:

我是 android 的初学者,想知道两者之间有什么区别 tools:contextandroid:name 属性?

 1. android:name=".fragments.DataFragment"
 2. tools:context=".activities.MainActivity"

完整代码:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment"
    android:name=".fragments.DataFragment"
    tools:layout="@layout/data_fragment"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MainActivity"
   />

【问题讨论】:

    标签: android android-layout android-layout-editor android-tools-namespace


    【解决方案1】:

    &lt;fragment 标记的情况下,android:name 属性告诉LayoutInflater 在此布局膨胀时要实例化什么Fragment 类。

    tools:context 标签只是通知布局编辑器这个布局应该在哪里使用。这样编辑器就可以从该 Activity 中提取主题,以显示更准确的预览(例如强调色、文本样式)。由于一个布局可以在多个地方重复使用,它只是向 IDE 提供如何呈现它的提示。

    【讨论】:

      猜你喜欢
      • 2010-10-10
      • 2020-04-03
      • 2011-11-14
      • 2016-04-29
      相关资源
      最近更新 更多