Android onConfigurationChanged新布局未更新

时间:2016-05-11 13:48:51

标签: android android-layout

我需要在方向更改时替换我的布局并保持我的视图状态和监听器(例如在纵向模式下我将TextView颜色从蓝色更改为红色,当我旋转设备时,需要使用相同TextView的新布局红色)。我有2个布局:一个布局/一个布局-land /我在AndroidManifest.xml中添加了这个:android:configChanges="orientation|keyboardHidden|screenSize" 这是我的onConfigurationChanged

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

    mDisplayWidth = Helper.getDisplayWidth(context);
    mDisplayHeight = Helper.getDisplayHeight(context);
    mDisplayOrientation = getResources().getConfiguration().orientation;

    if(mDisplayOrientation == Configuration.ORIENTATION_LANDSCAPE){
        //landscape

    }else{
        //portret
    }

    final View newView = View.inflate(getActivity(), R.layout.harta, null);
    ViewGroup rootView = (ViewGroup) getView();
    rootView.removeAllViews();
    rootView.addView(newView);
}

问题在于新布局不能保留旧状态和听众,我该如何实现?

1 个答案:

答案 0 :(得分:0)

从清单中删除android:configChanges="orientation|keyboardHidden|screenSize"。否则onConfigurationChanged()不会导致;

相关问题