问题
Creating an activity with navigation drawer in Android Studio causes this to be shown in the XML code:
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
If I remove the tools:openDrawer="start"
, on the layout preview, the navigation drawer will be closed instead of open.
I thought this was a replacement attribute, similar to android:text
that can be replaced with tools:text
to put some placeholder text in the layout preview. But when I change it to android:openDrawer
or app:openDrawer
, the app can't be compiled because they are unknown attributes.
So I wonder how tools:openDrawer
works, and whether it is documented anywhere.
回答1:
Android has a dedicated XML namespace intended for tools to be able to record information in XML files, and have that information stripped when the application is packaged such that there is no runtime or download size penalty. The namespace URI is http://schemas.android.com/tools and is usually bound to the tools: prefix:
This means that tools
attribute is used by the IDE and not used in the code functionality.
I am still trying to find the complete documentation of all tools
attributes. I can't find out the openDrawer
attribute anywhere. But the following are great documentation on tools
:
http://tools.android.com/tech-docs/tools-attributes
http://tools.android.com/tips/layout-designtime-attributes
UPDATE
Now a complete documentation of all tools
attribute is available here.
回答2:
It makes the navigation drawer visible on the preview screen in Android Studio essentially mimicking the user swiping the drawer onto the screen.
Without this attribute, you would see only the content view of the main screen.
I haven't tried it but I suspect substituting "start" for "end" would make the drawer on the right of the screen open (assuming there is one of course).
来源:https://stackoverflow.com/questions/33341472/whats-the-purpose-of-toolsopendrawer-start-when-creating-a-drawerlayout-wit