我想为我的应用程序设计一个动态UI,以便在500dp
宽度大于屏幕的屏幕上显示2个片段,否则显示单个片段。
这是我的布局文件:
布局/ activity_main:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
layout-w500dp / main_activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="ir.focusmag.focus.PostsFragment"
android:id="@+id/posts_frag_container"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
tools:layout="@layout/fragment_posts" />
<fragment android:name="ir.focusmag.focus.PhotosFragment"
android:id="@+id/photos_frag_container"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
tools:layout="@layout/fragment_photos" />
</LinearLayout>
在MainActivity.java
其中检查是否frag_container
存在以及是否存在,它将装入PostsFragment
其中:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check whether the activity is using the layout version with
// the fragment_container FrameLayout. If so, we must add the first fragment
if (findViewById(R.id.frag_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
firstFragment = new PostsFragment();
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragm_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.frag_container, firstFragment).commit();
}
}
还有一个侦听器,用于单击帖子以将片段替换为详细信息片段。
这里的重点是,如果我们要从以前的状态中恢复,它什么也不会做。
在通常情况下,此方法有效,但是如果用户在横向模式(屏幕宽度> 500dp)中为应用程序加注星标,则MainActivity会检查是否存在frag_container
,因为它不存在,它将不起作用,然后用户将设备旋转至纵向模式,而savedInstanceState
不会不再为null frag_container
。
如果我取消对的检查savedInstanceState
,则每当用户旋转设备时,PostsFragment就会被加载,并且不需要它。例如,用户正在检查详细信息并旋转设备,它将显示帖子而不是详细信息。
我该怎么做才能停止这种行为?
我正在按照Android Developer的教程进行操作。
提前致谢
这种方法有些过时了。如果您刚刚开始使用Android玩并且想了解如何以现代方式处理事情,请观看Google提供的Google开发Android Apps视频课程。他们将向您展示如何正确处理“主/详细”模式。如果您想立即跳转到代码,请启动Android Studio并Activity
使用向导创建新代码,然后选择“主/详细信息”表单。
您的新活动的onCreate()方法如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
if (findViewById(R.id.item_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-large and
// res/values-sw600dp). If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
// In two-pane mode, list items should be given the
// 'activated' state when touched.
((ItemListFragment) getFragmentManager()
.findFragmentById(R.id.item_list))
.setActivateOnItemClick(true);
}
// TODO: If exposing deep links into your app, handle intents here.
}
与您当前的方法相比,这次将单窗格模式片段添加到XML布局中,因此您不必为意外的行为而烦恼。您还将在那里看到布局别名的工作方式。
我知道这不是一个简单的答案,您可能希望购买它可能是您的应用程序应用此更改的最后时刻。我真的鼓励你这样做。
如果您不能选择此更改,请考虑修改单个窗格的布局,并在其中添加片段,而不是动态添加。目前,您可能根本无法从动态片段中受益。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句