安卓系统的布局定义在两个独立的文件中



我发现我的活动布局是在两个文件中定义的。这是我在/res/menu:中的main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- The main content view -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <!-- The navigation drawer -->
        <ListView android:id="@+id/left_drawer"
            style="@style/NavigationDrawer"/>
            <!--  android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#111"/> -->
    </android.support.v4.widget.DrawerLayout>

    <item
        android:id="@+id/add_button"
        android:icon="@drawable/ic_action_new"
        android:title="@string/add_button"
        android:showAsAction="ifRoom"/>
</menu>

在这里,您可以在/res/layout/子文件夹中找到我的activity_main.xml。

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg">

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        style="@style/NavigationDrawer"/>
</android.support.v4.widget.DrawerLayout>

如您所见,例如,导航抽屉被定义了两次。这有必要吗?我必须把每一个更改都写入这两个文件吗?

从main.xml中删除以下内容。如果在布局文件中进行更改,则无需在main.xml中进行任何更改。

检查以下链接以了解布局。activity_main.xml是布局文件

http://developer.android.com/guide/topics/ui/declaring-layout.html

main.xml是菜单资源

http://developer.android.com/guide/topics/ui/menus.html

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- The main content view -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <!-- The navigation drawer -->
        <ListView android:id="@+id/left_drawer"
            style="@style/NavigationDrawer"/>
            <!--  android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#111"/> -->
    </android.support.v4.widget.DrawerLayout>

最新更新