Android 片段| | OnConfiguration已更改| |重复

Android 片段| | OnConfiguration已更改| |重复,android,fragment,Android,Fragment,当屏幕方向改变时,我遇到了碎片问题。 在我的代码中,我根据屏幕方向创建了不同的布局xml。 例如,我在布局目录中有header_横向.xml和header_纵向.xml。每个不同的标题在线性布局中都有相同的片段。所以当我打开我的设备,我有错误的重复id。。。与我的片段相对应 布局之间的区别在于内容。当我在横向时,我显示的信息比纵向时更多 创建我的活动时: setContentView(R.layout.main_landscape); //header

当屏幕方向改变时,我遇到了碎片问题。 在我的代码中,我根据屏幕方向创建了不同的布局xml。 例如,我在布局目录中有header_横向.xml和header_纵向.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兼容性库片段开发了PP

对不起我的英语


谢谢

你应该让android为你做所有的配置切换!不要在onConfigurationChanged中执行任何操作,甚至从清单中删除所有配置更改

把你的肖像画放进去 布局/文件夹

把你的景观布局放进去 布局土地/文件夹

确保它们都命名为: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等文件夹中。通过这种方式,您可以非常轻松地拥有多个布局。

您应该让android为您完成所有配置切换!不要在onConfigurationChanged中执行任何操作,甚至从清单中删除所有配置更改

把你的肖像画放进去 布局/文件夹

把你的景观布局放进去 布局土地/文件夹

确保它们都命名为: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等文件夹中。通过这种方式,您可以非常轻松地拥有多个布局。

如果出于某种原因需要自己处理方向更改,解决方案是在调用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
    ...
}

如果出于某种原因需要自己处理方向更改,解决方案是在调用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
    ...
}

您的意思是我必须复制所有布局并创建两个文件夹布局区域和布局端口吗?但是碎片以两种不同的布局出现?感谢您的帮助。我已经尝试了我们的解决方案,但我的标题在纵向和横向中是相同的,我如何更改它?它工作正常,但活动应该重新启动,我丢失了配置。所以我应该保存数据和对象,有办法恢复所有数据吗?你把肖像布局放在/layout/里,把风景布局放在/layout land/里。它们可以完全不同,只是需要相同的名称。若要保存和还原数据,请将数据保存在onSaveInstanceState中,并在捆绑时将其还原到onCreate中!=你是说我必须复制所有布局并创建两个文件夹布局区域和布局端口?但是碎片以两种不同的布局出现?感谢您的帮助。我已经尝试了我们的解决方案,但我的标题在纵向和横向中是相同的,我如何更改它?它工作正常,但活动应该重新启动,我丢失了配置。所以我应该保存数据和对象,有办法恢复所有数据吗?你把肖像布局放在/layout/里,把风景布局放在/layout land/里。它们可以完全不同,只是需要相同的名称。若要保存和还原数据,请将数据保存在onSaveInstanceState中,并在捆绑时将其还原到onCreate中!=无效的