0

我的 Android 项目有问题。我正在为手机和平板电脑创建不同的布局,但有一些问题。这些布局使用不同的片段。这是我的 MainActivity onCreate 方法:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);

    if (isTablet(this.getApplicationContext()) &&
            getResources().getConfiguration().orientation
            == Configuration.ORIENTATION_LANDSCAPE){
        mNavigationFragment = (NavigationFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation);

    } else {
        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

    }

    mTitle = getTitle();

    getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {

        @Override
        public void onBackStackChanged() {
            Fragment f = getSupportFragmentManager().findFragmentById(R.id.container);
            if (f != null){
                updateTitleAndDrawer (f);
            }

        }
    });
}

这是 isTablet 方法:

public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

但关键是,当我改变方向时,布局不会改变。你能告诉一些建议吗?

忘了提,我在 AndroidManifest 中有以下行

android:configChanges="keyboardHidden|orientation|screenSize">
4

1 回答 1

2

你应该添加到你的 mainActivity:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

}

并添加到您的清单中:

<activity
    android:configChanges="orientation"
</activity>
于 2015-02-05T13:08:51.847 回答