【问题标题】:Android Fragments: fragments inside a Frame or other layouts?Android Fragments:Frame 或其他布局内的片段?
【发布时间】:2014-09-12 12:17:29
【问题描述】:

我现在正在创建一个 Android 应用程序,并且我有一个框架,其中外壳包含片段。到目前为止一切正常,但我遇到了一个我无法完全理解的问题。

我有一个 Frame,它是整个屏幕(FrameLayout - 我应该使用 FrameLayout 作为主 Frame 吗?)并且在这个 Frame 内部有一些片段会根据用户的交互而改变。这些片段位于 .xml 文件 FrameLayouts 中。我现在想知道它们是否可以或应该是 FrameLayouts 或 Fragments...我创建了一个 Google Maps frgment,它是一个 Fragment ideed,这让我思考。

所以我的问题是:它是否会产生影响或对性能有任何影响,或者我可以简单地使用任何符合其目的的东西吗?

为了说明我的意思,这里有一些代码示例: (FrameLayout里面FrameLayout里面都是放的fragment)

主框架(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>

一个片段(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 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文件的内容吗?

标签: java android performance fragment android-framelayout


【解决方案1】:

如果您在 xml 中附加片段,如 GoogleMapFragment 或在 xml 中使用 FrameLayout(或任何其他 ViewGroup,如 LinearLayout 或 RelativeLayout)。两种方式都是一样的。更有可能以编程方式(即在 Java 中)创建文本视图或在 xml 中定义它。

在 Java 中使用片段:

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 是你的框架布局的 id。

在 XML 中使用片段:

<fragment android:name="com.example.ExampleFragment"
            android:id="@+id/fragment"               
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

如果是 XML 中的 Fragment,您的 Fragment 将由 &lt;fragment/&gt; xml 标记定义,而另一方面,当我们以编程方式使用 Fragment 时,您的 FrameLayout 将用作您的 Fragment 的容器。

结论:使用 FrameLayout 会增加一个层次结构,但不会对您的性能产​​生太大影响。

见:http://developer.android.com/guide/components/fragments.html#Adding

中的android:name属性指定Fragment 在布局中实例化的类。当系统创建这个 活动布局,它实例化布局中指定的每个片段 并为每个调用 onCreateView() 方法,以检索每个 片段的布局。系统插入返回的 View 片段直接代替元素。

【讨论】:

  • 好的,我已多次阅读您的回答,但无法完全理解。你能再详细一点吗?我对 android 很陌生...
  • @JRsz:在xml中使用FrameLayout和fragment的区别是你的问题吧?
  • @JRsz:我已经更新了我的答案,我认为现在它更容易理解了。
  • 好的,谢谢!我想我会使用 FrameLayout 版本:)
猜你喜欢
  • 2012-09-19
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
  • 2017-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多