我有FragmentActivity
使用2种不同的布局取决于屏幕尺寸:
xLarge Landscape。布局有3个LinearLayouts
,每个包含1个片段。
休息屏幕尺寸。布局有1 ViewPager
,其中包含所有片段。
当屏幕方向在10英寸屏幕中发生变化时,应用程序崩溃。它在横向模式下使用屏幕尺寸1,在纵向模式下使用尺寸2。
代码:
public class RegistroAccionesPartidoActivity extends FragmentActivity {
private static ArrayAdapter<Fragment> mFragmentArray;
private ViewPager mMyPager;
private Fragment1 mFragment1;
private Fragment2 mFragment2;
private Fragment3 mFragment3;
private MyFragmentAdapter mFragmentsAdapter;
private FragmentManager mFragmentManager;
private FragmentTransaction mFragmentTrasaction;
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.my_layout);
LinearLayout layoutFragment1 = (LinearLayout)findViewById(R.id.fragment1);
if(layoutFragment1 == null) {
mFragmentManager = getSupportFragmentManager();
mMyPager = (ViewPager) findViewById(R.id.my_pager);
mFragmentArray = new ArrayAdapter<Fragment>(getApplicationContext(),android.R.layout.simple_list_item_1);
mFragmentsAdapter = new Fragment2Adapter(mFragmentManager);
if(savedState == null) {
mFragment2 = new Fragment2(true);
mFragment3 = new Fragment3();
mFragment1 = new Fragment1(mIdPartido, mIdEquipoLocal, mIdEquipoVisitante);
} else {
mFragment1 = (Fragment1) getSupportFragmentManager().getFragment(savedState, "fragment1");
mFragment2 = (Fragment2) getSupportFragmentManager().
getFragment(savedState,"fragment2");
mFragment3 = (Fragment3) getSupportFragmentManager().
getFragment(savedState, "fragment3");
}
mFragmentArray.add(mFragment1);
mFragmentArray.add(mFragment2);
mFragmentArray.add(mFragment3);
mDatosPartidoPager.setAdapter(mFragmentsAdapter);
mDatosPartidoPager.setCurrentItem(1);
} else { // xLarge Version
mFragmentManager = getSupportFragmentManager();
mFragmentTrasaction = mFragmentManager.beginTransaction();
if(savedState == null) {
mFragment1 = new Fragment1();
mFragment2 = new Fragment2();
mFragment3 = new Fragment3();
} else {
mFragment1 = (Fragment1) getSupportFragmentManager().
getFragment(savedState, "fragment1");
mFragment2 = (Fragment2) getSupportFragmentManager().
getFragment(savedState,"fragment2");
mFragment3 = (Fragment3) getSupportFragmentManager().
getFragment(savedState, "fragment3");
}
mFragmentTrasaction.add(R.id.fragment1, mFragment1);
mFragmentTrasaction.add(R.id.fragment2, mFragment2);
mFragmentTrasaction.add(R.id.fragment3, mFragment3);
mFragmentTrasaction.commit();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "fragment2" ,
mFragment2);
getSupportFragmentManager().putFragment(outState, "fragment3",
mFragment3);
getSupportFragmentManager().putFragment(outState,"fragment1",
mFragment1);
}
}
XLarge布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
其余屏幕尺寸布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_pager">
</android.support.v4.view.ViewPager>
</LinearLayout>
根据屏幕更改的顺序,我有2次不同的崩溃:
java.lang.IllegalArgumentException:找不到ID为0x的视图......对于Fragment2
java.lang.IllegalStateException:已添加片段:Fragment1
抱歉这个大问题。我只是想明确解释我的问题。
非常感谢。
编辑:在大型设备(如10'')中,横向模式使用xLarge布局,纵向模式使用其他布局。
答案 0 :(得分:0)
对我来说(在XLarge布局内)嵌套的LinearLayout-Elements混合水平和垂直方向看起来很奇怪。如果你想以这种方式嵌套,请仔细看看。
问候, 沃尔夫冈