我现在正在创建一个 Android 应用程序,我有一个 Frame,它的外壳包含 fragment 。到目前为止一切正常,但我遇到了一个我无法完全理解的问题。
我有一个 Frame,它是整个屏幕(FrameLayout - 我应该使用 FrameLayout 作为主框架吗?)并且在这个 Frame 内部有一些 fragment 会根据用户交互而改变。这些 fragment 位于 .xml 文件 FrameLayouts 中。我现在想知道它们是否可以或应该是 FrameLayouts 还是 fragment ...我创建了一个 Google Maps fragment ,这是一个 fragment ,这让我开始思考。
所以我的问题是:它是否会产生影响或对性能产生任何影响,或者我可以简单地使用任何服务于它的目的吗?
为了说明我的意思,这里有一些代码示例: (FrameLayout里面的FrameLayout里面都是fragments放的)
主框架(activity_main.xml):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >
<FrameLayout
android:id = "@+id/mainFrame"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_marginBottom = "@dimen/bottom_Main_Tabs">
</FrameLayout>
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "@dimen/bottom_Main_Tabs"
android:layout_gravity = "bottom"
android:background="@color/grey2"
>
<ImageButton
android:id = "@+id/bottomButton_home"
android:layout_height = "match_parent"
android:layout_width = "0dp"
android:layout_weight = "1.0"
android:layout_marginLeft = "2dp"
android:layout_marginRight = "2dp"
android:background = "@drawable/ic_home_white"
android:onClick = "openHome"
/>
[...]
</LinearLayout>
<RelativeLayout
android:id = "@+id/TopBar"
android:layout_width = "match_parent"
android:layout_height = "@dimen/top_Bar_Hight"
android:layout_gravity="top"
android:background="@color/grey_transparentBy50"
>
<ImageView
android:id= "@+id/TopBarLogo"
android:layout_width = "@dimen/top_Bar_Hight"
android:layout_height = "@dimen/top_Bar_Hight"
android:background="@drawable/ic_launcher"
/>
[...]
</RelativeLayout>
</FrameLayout>
一个 fragment (FrameLayout:)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/home_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"
tools:context="com.domain.app.HomeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:textSize="100sp"
android:text="Home" />
</FrameLayout>
另一个 fragment ( fragment )
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.domain.app.MapsActivity"
android:name="com.google.android.gms.maps.MapFragment"
tools:layout = "@layout/activity_main"
/>
GoogleMaps 实现有太多问题,但我还没有花足够的时间在这里寻求帮助。
提前致谢。
约翰
最佳答案
如果您在 xml 中附加 fragment ,如 GoogleMapFragment 或在 xml 中使用 FrameLayout(或任何其他 ViewGroup,如 LinearLayout 或 RelativeLayout)。两种方式都是一样的。它更有可能以编程方式(即在 Java 中)创建 TextView 或在 xml 中定义它。
在 Java 中使用 fragment :
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
在xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
这里的 R.id.fragment_container 是你的 Frame Layout 的 id。
在 XML 中使用 fragment :
<fragment android:name="com.example.ExampleFragment"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
对于 XML 中的 fragment ,您的 fragment 将由 <fragment/>
定义xml 标记,另一方面,当我们以编程方式使用 fragment 时,您的 FrameLayout 将用作 fragment 的容器。
结论:使用 FrameLayout 会增加一个层次,但不会对您的性能产生太大影响。
参见:http://developer.android.com/guide/components/fragments.html#Adding
The android:name attribute in the specifies the Fragment class to instantiate in the layout. When the system creates this activity layout, it instantiates each fragment specified in the layout and calls the onCreateView() method for each one, to retrieve each fragment's layout. The system inserts the View returned by the fragment directly in place of the element.
关于java - 安卓 fragment : fragments inside a Frame or other layouts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25808175/