【问题标题】:How do I reuse the same xml background layout in multiple activities/fragments in Android Studio?如何在 Android Studio 的多个活动/片段中重用相同的 xml 背景布局?
【发布时间】:2017-06-21 06:27:03
【问题描述】:

我正在开发一个 Android 应用,它几乎在每个活动上都有相同的背景图像和底部导航栏。

我想重用这段代码,而不是每次都写出来:

这是我的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/background_image">



    <android.support.design.widget.BottomNavigationView
        android:layout_gravity="bottom"
        android:id="@+id/bottom_nav_bar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="@drawable/bottom_nav_menu_image"
    xmlns:android="http://schemas.android.com/apk/res/android" />

</LinearLayout>

这将是所有视图的基本背景;但是,我将为每个布局在其顶部放置不同的布局。是否可以使用插入和合并标签以这种方式开始每个新活动?

【问题讨论】:

  • 制作上述代码的 xml 并使用 并在应用程序 xml 的每个布局中使用这一行
  • 我在主 xml 布局中该行之后放置的所有内容都会被考虑在模板 xml 文件中的 LinearLayout 中吗?
  • @ScottWeller 您可以将包含布局放在任何父布局中,例如线性布局、相对布局等。是的,您可以在
  • @ScottWeller- 是的。这将在您的主要线性布局中考虑。

标签: android android-layout android-xml


【解决方案1】:

下面需要xml文件调用

  <include layout="@layout/custom_my_menus" />

这是我的 xml 文件编辑你想要这样。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="10">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/MyDarkToolbarStyle">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                android:gravity="end"
                android:orientation="horizontal"
                android:weightSum="10">


                <!--<ImageView-->
                <!--android:id="@+id/img_back"-->
                <!--android:layout_width="wrap_content"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:src="@drawable/ic_back" />-->

                <TextView
                    android:id="@+id/txt_header"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:layout_weight="7"
                    android:gravity="left"
                    android:text="Design Collaboration"
                    android:textColor="@color/white"
                    android:textSize="18sp" />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="3"
                    android:gravity="end">


                    <ImageView
                        android:id="@+id/img_upload"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_margin="8dp"
                        android:src="@drawable/upload_x24"
                        android:visibility="gone" />
                </LinearLayout>

            </LinearLayout>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="10"
        android:orientation="vertical">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs_collaboration"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/lightPrimary"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabIndicatorHeight="4dp"
            app:tabMaxWidth="0dp"
            app:tabMode="fixed" />

        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <include layout="@layout/custom_homeowner_menus" />
    <include layout="@layout/custom_professional_menus" />
</LinearLayout>

【讨论】:

  • 那部分我明白了。我的问题是如何将其包含在新活动中并在其之上构建? android studio 会假设我在 include 语句之后放置的任何代码都在 xml 文件的线性布局中吗?
  • @ScottWeller- 添加包含意味着您在“包含布局”中编写的任何代码都将表现得与她编写整个代码相同,而不是包含代码。据我说,你第一次尝试,你就会自然而然地知道它是如何工作的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-05
  • 2016-04-28
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
相关资源
最近更新 更多