【问题标题】:start a new activity in a include block在包含块中开始一个新活动
【发布时间】:2016-06-16 17:54:35
【问题描述】:

我的 XML 文件中有一个包含块我试图在这个包含中启动一个新活动,而不是全屏,更重要的是,它的行为类似于该部分中的当前布局。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.rt.giftcard.ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:background="@color/material_deep_teal_500">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>


    <include layout="@layout/content_scrolling"
        android:id="@+id/page_content" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end" />

我首先尝试更改包含的布局 也许碎片是解决方案,但我不知道如何?

【问题讨论】:

    标签: android layout


    【解决方案1】:

    是的,我认为您肯定希望使用片段而不是包含。您可以阅读更多关于 Fragments here 的信息,但这是您必须采取的几个步骤。

    1) 将您的 &lt;include&gt; 标签替换为 &lt;fragment&gt; 标签。定义您的属性,其中应包括名称和 ID。

    2) 定义您的 Fragment 类。 &lt;fragment&gt; 标记中的名称应该引用从 Fragment 继承的类。 Fragment 类似于典型的 Activity,因此学习曲线应该不会那么糟糕。

    3) 在父 Activity 中获取 FragmentManager 的一个实例,以便创建一个将片段添加到当前屏幕的 FragmentTransaction。听起来很多,我知道,但有人简要解释一下here

    我希望这会有所帮助。一旦你习惯了制作 Fragments,这应该是一件轻而易举的事。

    【讨论】:

    • 非常感谢。我会试试的,感觉就像在工作:)
    • 太棒了,如果正确,你能确定答案吗?谢谢老兄。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 2011-03-31
    • 2017-10-18
    相关资源
    最近更新 更多