What's tools:layout in fragment xml file?

亡梦爱人 提交于 2019-12-03 14:42:25

问题


I started a new Android Application based on master/detail flow template using ADT Eclipse. This template creates two activities, a master fragment and a detail fragment in order to fit small and larger screens.

I noticed the activity_item_list.xml file has the tools:layout attribute:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_list"
    android:name="com.example.fragmenttwopanel.ItemListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    tools:context=".ItemListActivity"
    tools:layout="@android:layout/list_content" />

I tried to delete the attribute and the app runs in the same way but in the graphical layout tab of ADT, a message ask me for:

pick preview layout from the "Fragment Layout" context menu

What is its purpose? Is it just for preview in graphical layout?


回答1:


This is a namespace used by the IDE tools. This is not needed to run the application on a device (or emulator). On the other hand, if you leave them, Android will ignore them when the application is running on a device.

This is used by e.g. lint and graphical layout tab.

You can check how it's used by lint here: http://developer.android.com/tools/debugging/improving-w-lint.html in section Configuring lint checking in XML.




回答2:


Yes, it's just information for the Graphical Layout editor how the fragment should be displayed in the editor.

Generally the tools attributes (with tools namespace prefix declared with xmlns:tools="http://schemas.android.com/tools") are used by the development tools and are not compiled in the application itself.




回答3:


right click on the grey area, " fragment layout->choose layout" and select your layout , DONE!



来源:https://stackoverflow.com/questions/19136163/whats-toolslayout-in-fragment-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!