我刚刚开始学习片段,并且已经读过了几个教程。我注意到的是,您可以用另一个片段替换现有的片段,或者引用现有的片段并在其上更改UI内容。我假设也有添加和删除片段的方法。
但在我开始深入学习片段之前,我假设Android有某种内置的片段选择机制。如果我的应用程序在智能手机上运行,它可能会为主屏幕选择一个片段,然后通过滑动显示第二个片段。这将提供一种主/详细视图。然后,如果在平板电脑上运行相同的应用程序,那么两个片段都会同时显示。
但显然我错了。Android可能没有足够复杂的内置逻辑来确定屏幕大小,并以某种优化的方式自动排列片段。是吗?还是我应该检测屏幕大小,并在代码中排列我的片段,使其优化屏幕使用?有了很多不同的屏幕大小,我怀疑我最终会写很多代码来确定每个大小的布局,并相应地排列片段。
发布于 2014-06-18 12:39:25
根据屏幕大小定义不同的布局,然后控制源代码中片段之间的交互。因此,在主/详细的情况下,您将有一个处理表的布局,另一个处理电话的布局。Android将根据当前设备的大小自动选择其布局文件。要查看一个示例,请在Eclipse中启动一个新项目,选择master/detail,它将创建一个简单的项目,正如所描述的那样。
以下内容摘自http://developer.android.com/guide/practices/tablets-and-handsets.html
根据屏幕大小,系统应用不同的main.xml布局文件:
res/layout/main.xml用于手机:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- "Fragment A" -->
<fragment class="com.example.android.TitlesFragment"
android:id="@+id/list_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </FrameLayout>res/layout-用于平板电脑的大型/main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/frags">
<!-- "Fragment A" --> <fragment class="com.example.android.TitlesFragment"
android:id="@+id/list_frag"
android:layout_width="@dimen/titles_size"
android:layout_height="match_parent"/>
<!-- "Fragment B" --> <fragment class="com.example.android.DetailsFragment"
android:id="@+id/details_frag"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </LinearLayout>发布于 2014-06-18 12:28:25
好的,这就是它的工作原理。
在活动的布局文件中有一个容器,每次添加/替换一个片段时,都会将它添加到该容器中。
如果你想在平板电脑上并排展示两个不同的碎片。您只需创建一个单独的XML,它将包含两个容器,并且您可以非常轻松地使用java处理它。
来自这里的更多参考Android片段
https://stackoverflow.com/questions/24285495
复制相似问题