【问题标题】:Android - layout is defined in two seperate files?Android - 布局是在两个单独的文件中定义的?
【发布时间】:2013-12-06 07:47:15
【问题描述】:

我发现我的活动布局是在两个文件中定义的。这是我在 /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>

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

【问题讨论】:

标签: android xml layout styles


【解决方案1】:

从 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>

【讨论】:

  • 嗯,这似乎合乎逻辑。不知道如何将所有这些添加到菜单资源中。
  • @user2971688 您不需要将所有这些添加到菜单资源中。检查mypost中的链接
猜你喜欢
  • 2011-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
相关资源
最近更新 更多