我在android 清单中使用了android:configChanges="orientation|screenSize"
,因为当我旋转设备时,我的设备不会在layout-land文件夹中选择布局XML文件我有什么问题?
答案 0 :(得分:2)
如果你选择使用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);
}
}
我已经在您可以找到here的项目中编写了上述示例,您可以更好地了解它的工作原理。希望它有所帮助。