当屏幕方向改变时,我遇到了fragment的问题。在我代码中,我根据屏幕方向创建了不同的布局xml。例如,我在布局目录中有header_landscape.xml和header_portrait.xml。并且每个不同的报头在线性布局中具有相同的片段。因此,当我打开我的设备时,我会看到错误"duplicate id ...“与我的片段相对应。
布局的不同之处在于我在“风景”中的content.When比在“肖像”中显示的信息更多。
在创建我的活动时:
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 (兼容性库片段)进行开发。
对不起,我的英语不好。
谢谢
发布于 2012-02-23 23:25:06
你应该让android为你做所有的配置切换!不要在"onConfigurationChanged“中做任何事情,甚至从清单中删除所有配置更改!
将您的纵向布局放在 layout / folder中
并将您的景观布局放在 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.等文件夹中这样你就可以很容易地拥有多个布局。
发布于 2012-04-06 18:48:03
如果您出于某种原因需要自己处理方向更改,一种解决方案是在调用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
...
}https://stackoverflow.com/questions/9415574
复制相似问题