在配置更改时,在android中布局不会自动更改

在配置更改时,在android中布局不会自动更改,android,Android,我在android清单中使用了一个android:configChanges=“orientation | screenSize”,因此当我旋转我的设备时,我的设备不会在layout land文件夹中选择布局XML文件。有人能告诉我问题出在哪里吗?如果你选择使用android:configChanges=“orientation | screenSize”然后在屏幕旋转时手动更新布局 @Override public void onConfigurationChanged(Configurati

我在android清单中使用了一个
android:configChanges=“orientation | screenSize”
,因此当我旋转我的设备时,我的设备不会在layout land文件夹中选择布局XML文件。有人能告诉我问题出在哪里吗?

如果你选择使用android:configChanges=“orientation | screenSize”然后在屏幕旋转时手动更新布局

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

    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        setContentView(R.layout.portrait);
    }
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        setContentView(R.layout.landscape);
    }
}
编辑: 使用片段会使事情复杂化,使用android:configChanges=“orientation | screenSize”无论如何都不推荐。您应该在片段中使用onSaveInstanceState保存片段的状态,然后在onCreateView中恢复片段状态:

public class DetailsFragment extends Fragment
{
    private EditText ed;

    public static DetailsFragment newInstance(int index)
    {
        return new DetailsFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_layout, container, false);
        ed = (EditText) view.findViewById(R.id.fragment_edit);

        if(null != savedInstanceState)
        {
            //restore the text if null != savedInstanceState -> fragment is recreated
            ed.setText(savedInstanceState.getString("RESTORE_TEXT"));
        }

        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        if(null != ed)
        {
            //save the text written in the edittext
            outState.putString("RESTORE_TEXT", ed.getText().toString());
        }

        super.onSaveInstanceState(outState);
    }
}

我在一个项目中写了上面的例子,你可以找到,你可以更好地看到它是如何工作的。希望有帮助。

我在android清单中使用的是android:configChanges=“orientation | screenSize”——这是你的问题。停止使用该文件。在纵向和横向布局中使用相同名称的XML文件。不,必须使用它。我不能停止使用ITAM做相同的事情,但由于配置更改,android没有拾取布局区域文件夹XML文件。如何创建布局文件夹。在创建文件夹时是否设置为更改横向模式?但在片段中没有方法SetContentView我在你的问题中没有看到你提到你使用的是片段。对不起,我的错误,但我使用的是片段。在这种情况下,我会做什么