【问题标题】:reuse parent layout in Android在 Android 中重用父布局
【发布时间】:2016-02-12 22:49:47
【问题描述】:

我的应用由多个活动组成,它们都有一个共同的布局,即抽屉布局,有一个工具栏,但它们有不同的主要内容。具体来说,它们的完整布局如下

<?xml version="1.0" encoding="utf-8"?>
<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">

    <RelativeLayout
        android:id="@+id/relative_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/action_bar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            />

        .......main content, this is different for each activity..........

    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

由于共享组件是在“父级”定义的,我不确定如何构建这些布局,以便通用布局只定义一次。有可能实现吗?谢谢。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    两种选择:

    1。 ViewStub

    使用ViewStub 定义主布局,您将在其中拥有不同的子布局。

    ViewStub 是一个不可见的、大小为零的 View,可用于延迟 在运行时膨胀布局资源。当 ViewStub 可见时, 或者当调用 inflate() 时,布局资源被膨胀。这 ViewStub 然后用膨胀的 View 替换其父级中的自身或 观看次数。

    在每个 Activity 中设置相同的布局,然后将特定的子布局膨胀到 ViewStub 上。

    2。碎片

    如果您想避免在单个 Activity 上包含大量代码,您可能需要考虑使用 Fragments


    其他一些可以指导您的主题:

    【讨论】:

      【解决方案2】:

      是的,可以使用 Fragment。对于这个结构,首先你需要在你的 activity_main.xml 中定义一个 FrameLayout 。随着时间的推移,这个框架布局将被片段的内容所取代。

      <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">
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
      
          <LinearLayout
              android:id="@+id/container_toolbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
              <include
                  android:id="@+id/toolbar"
                  layout="@layout/toolbar" />
          </LinearLayout>
      
          <FrameLayout
              android:id="@+id/container_body"
              android:layout_width="fill_parent"
              android:layout_height="0dp"
              android:layout_weight="1" >
          </FrameLayout>
      
        </LinearLayout>
      
        <fragment
          android:id="@+id/fragment_navigation_drawer"
          android:name="com.safallwa.zahan.oreader.FragmentDrawer"
          android:layout_width="@dimen/nav_drawer_width"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          app:layout="@layout/fragment_navigation_drawer"
          tools:layout="@layout/fragment_navigation_drawer" />
      </android.support.v4.widget.DrawerLayout> 
      

      然后,您可以根据需要创建布局和扩展 Fragment 的类。将这些新布局设置为 Fragment 类的 contentView。现在这些片段可以替换FrameLayoutactivity_main。假设我们有一个 Framgent 类名First_Fragment.Java,那么我们可以通过以下代码替换 activity_main framelayout 来显示该片段

              FragmentManager fragmentManager = getSupportFragmentManager();
              FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
              fragmentTransaction.replace(R.id.container_body, fragment);//container_body is the id of the framelayout
              fragmentTransaction.commit();
      

      对于带有导航抽屉的全功能示例,我们也可以点击以下链接

      http://www.codedisect.com/?p=134

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-10
        • 1970-01-01
        • 1970-01-01
        • 2011-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多