【问题标题】:Android - Supporting a tablet only app on phonesAndroid - 在手机上支持仅平板电脑的应用程序
【发布时间】:2013-01-04 19:04:21
【问题描述】:
我有一个目前仅在平板电脑 (Honeycomb+) 上受支持的应用程序。应用程序中的典型屏幕在 Activity 中有 2 个片段,就像新闻阅读器应用程序一样。 Fragment A 的点击会在 Fragment B 中显示相应的内容。现在的问题是,我如何在手机上支持这个应用程序(Froyo+)?这与支持来自活动的片段正好相反。任何形式的帮助/建议都非常感谢。
【问题讨论】:
标签:
android
android-fragments
android-activity
android-fragmentactivity
【解决方案1】:
你必须这样做:
layout/main.xml:
<LinearLayout
android:id="@+id/handset"
[...]
>
</LinearLayout>
layout-large/sw400dp:
<LinearLayout
[...]
>
<fragment android:name="com.bla.bla.FirstFragment"
android:id="@+id/first_fragment"
[...]
/>
<fragment android:name="com.bla.bla.SecondFragment"
android:id="@+id/second_fragment"
[...]
/>
</LinearLayout>
- 现在在 FragmentActivity 中检查这一点:
if (findViewById(R.id.handset) != null) {
// it's a handset device and you can add a Fragment to this View
}
FirstFragment firstFragment = new FirstFragment();
getSupportFragmentManager().beginTransaction().add(R.id.handset, firstFragment).commit();
- 如果
R.id.handset 返回null 那么它是一个平板电脑,在这种情况下,静态添加的片段将由它们的片段类处理。
【解决方案2】:
在手机上,每次点击更新平板电脑上的片段都会启动一个包含该片段的新活动。
这或多或少是唯一要做的事情:
* 在一个活动中封装较少的片段,通常是一个片段
* 将这些新活动联系在一起。
您的活动将只是一个新的粘合剂,您将重复使用片段。
另外,请注意平板电脑应用程序中片段之间的通信,您必须在活动中移动其中的一部分。