[英]Android Fragments: fragments inside a Frame or other layouts?
我现在正在创建一个Android应用,并且我有一个Frame,其中的外壳包含片段。 到现在为止一切正常,但是我遇到了一个我无法完全理解的问题。
我有一个框架,它是整个屏幕(框架布局-我应该使用框架布局作为主框架吗?),并且在此框架内,有一些片段会根据用户的交互而变化。 这些片段位于.xml文件FrameLayouts中。 我现在想知道它们是否可以或者应该是FrameLayouts或fragments ...我创建了一个Google Maps碎片,它是一个理想的碎片,这让我开始思考。
因此,我的问题主义者:这会有所不同还是会对性能产生任何影响?或者我可以简单地使用符合其目的的任何东西吗?
为了说明我的意思,下面是一些代码示例:(在Framelayout内的FrameLayout内放有所有片段)
MainFrame(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中附加片段,例如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/>
xml标签定义,另一方面,当我们以编程方式使用片段时,您的FrameLayout将用作片段的容器。
结论:使用FrameLayout将层次结构增加一个,但是不会对您的性能产生很大的影响。
请参阅: http : //developer.android.com/guide/components/fragments.html#Adding
中的android:name属性指定要在布局中实例化的Fragment类。 系统创建此活动布局时,将实例化布局中指定的每个片段,并为每个片段调用onCreateView()方法,以检索每个片段的布局。 系统直接将片段返回的视图插入到元素中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.