【发布时间】:2016-03-14 21:11:15
【问题描述】:
当我在 android studio 中创建带有片段的空白 Activity 时,实际做了什么!他们具体是怎么连在一起的?在哪一行代码中以及这是如何完成的?
【问题讨论】:
标签: android android-fragments android-studio fragment android-fragmentactivity
当我在 android studio 中创建带有片段的空白 Activity 时,实际做了什么!他们具体是怎么连在一起的?在哪一行代码中以及这是如何完成的?
【问题讨论】:
标签: android android-fragments android-studio fragment android-fragmentactivity
实际做了什么?
它从以下文件中创建一个模板。
Activity
ActivityFragment
strings.xml 值res/menu 资源<activity> 部分添加到AndroidManifest.xml
它们是如何连接在一起的?在哪一行代码?
在onCreate为Activity,你设置布局
setContentView(R.layout.activity_main);
像这样附加带有<fragment>标签的Fragment,基本上就是FrameLayout。
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.androidstack.app.MainActivityFragment"
tools:layout="@layout/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
【讨论】:
请阅读here。谷歌的文档应该可以帮助你理解一点。
但是一个简短的版本是这样的:
在活动的onCreate:
在片段的onAttach:
在片段的onCreate:
在 Fragment 的onViewCreated:
【讨论】:
只需将其视为活动中的活动。这与使用intent 更改activity 基本相同,但只是在其中。所以有一个parent activity,里面是frame 或layout 保存片段。当parent activity 完成加载时,它将开始加载layout 的内容,这是您的片段。 fragment的内容可以通过v.OnclickListener等方式访问
【讨论】: