屏幕方向改变时,片段出现问题。 在我的代码中,我根据屏幕方向创建了不同的布局xml。 例如,我在布局目录中有header_landscape.xml和header_portrait.xml。并且每个不同的头部在线性布局中具有相同的片段。因此,当我转动我的设备时,我的错误“重复ID ...”对应于我的片段。
布局之间的差异就是内容。当我在“风景”中时,我显示的信息比“肖像”中的信息要多。
创建我的活动:
setContentView(R.layout.main_landscape);
//header
date=(TextView)findViewById(R.id.headerLandscapeDate);
routeSens=(TextView) findViewById(R.id.headerLandscapeRouteSens);
pkHeader=(TextView) findViewById(R.id.headerLandscapePk);
//Récupération de la listview créée dans le fichier main.xml
maListViewPerso = (ListView) findViewById(R.id.ListeChoix);
//On attribut à notre listView l'adapter que l'on vient de créer
maListViewPerso.setAdapter(chargeMenu());
$方向屏幕更改时修改布局的代码。
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE && isLoaded && oldScreenStateOrientation!=newConfig.orientation)
{
setContentView(R.layout.main_landscape);
//header
date=(TextView)findViewById(R.id.headerLandscapeDate);
routeSens=(TextView) findViewById(R.id.headerLandscapeRouteSens);
pkHeader=(TextView) findViewById(R.id.headerLandscapePk);
maListViewPerso = (ListView) findViewById(R.id.ListeChoix);
//On attribut à notre listView l'adapter que l'on vient de créer
maListViewPerso.setAdapter(chargeMenu());
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT && isLoaded && oldScreenStateOrientation!=newConfig.orientation)
{
setContentView(R.layout.main_portrait);
routeSens=(TextView) findViewById(R.id.headerPortraitRouteSens);
pkHeader=(TextView) findViewById(R.id.headerPortraitPk);
maListViewPerso = (ListView) findViewById(R.id.ListeChoix);
//On attribut à notre listView l'adapter que l'on vient de créer
maListViewPerso.setAdapter(chargeMenu());
}
}
如果有人有解决方案。 我使用api8(兼容性库片段)开发。
抱歉我的英文。
由于
答案 0 :(得分:3)
如果由于某种原因需要自己处理方向更改,解决方法是在调用setContentView()之前删除片段。以下是我的案例中的代码:
@Override
public void onConfigurationChanged(final Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// We must remove the fragment, otherwise we get a duplicate ID error when the
// onCreateView() method is executed.
final FragmentManager fm = this.getSupportFragmentManager();
ExtrasFragment ef = (ExtrasFragment) fm.findFragmentByTag(ExtrasFragment.FRAGMENT_TAG);
if( ef != null ) { // for small screens the fragment is not embedded in this activity
final FragmentTransaction ft = fm.beginTransaction();
ft.remove(ef);
ft.commit();
ef = null;
fm.executePendingTransactions();
}
this.setContentView(R.layout.main); // contains the ExtrasFragment
...
}
答案 1 :(得分:1)
你应该让android为你做所有的配置切换!不要在“onConfigurationChanged”中执行任何操作,甚至从清单中删除所有配置更改!
将您的人像布局放入 布局/文件夹
并将您的风景布局放入 layout-land / folder
确保它们都被命名为: main.xml (或任何东西,只要它们是相同的)
然后在 activity.onCreate 中,执行以下操作:
// Will automatically select the correct layout
setContentView(R.layout.main);
//header
// If this is in portrait, date will be null. Check for that later
date=(TextView)findViewById(R.id.headerDate);
routeSens=(TextView) findViewById(R.id.headerRouteSens);
pkHeader=(TextView) findViewById(R.id.headerPk);
//Récupération de la listview créée dans le fichier main.xml
maListViewPerso = (ListView) findViewById(R.id.ListeChoix);
//On attribut à notre listView l'adapter que l'on vient de créer
maListViewPerso.setAdapter(chargeMenu());
这样,你可以让android担心使用哪种配置。如果您只想在大屏幕上显示横向视图,可以将其放在 layout-w1024dp 等文件夹中。这样,您可以非常轻松地拥有多个布局。