首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >片段|| onConfigurationChanged ||重复

片段|| onConfigurationChanged ||重复
EN

Stack Overflow用户
提问于 2012-02-23 23:05:47
回答 2查看 4.5K关注 0票数 2

当屏幕方向改变时,我遇到了fragment的问题。在我代码中,我根据屏幕方向创建了不同的布局xml。例如,我在布局目录中有header_landscape.xml和header_portrait.xml。并且每个不同的报头在线性布局中具有相同的片段。因此,当我打开我的设备时,我会看到错误"duplicate id ...“与我的片段相对应。

布局的不同之处在于我在“风景”中的content.When比在“肖像”中显示的信息更多。

在创建我的活动时:

代码语言:javascript
复制
 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());

$方向屏幕改变时修改布局的代码。

代码语言:javascript
复制
 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 (兼容性库片段)进行开发。

对不起,我的英语不好。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-23 23:25:06

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

将您的纵向布局放在 layout / folder

并将您的景观布局放在 layout -land/ folder

确保它们都被命名为: main.xml (或者任何名称,只要是相同的)

然后在您的activity.onCreate中,执行以下操作:

代码语言:javascript
复制
    // 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.等文件夹中这样你就可以很容易地拥有多个布局。

票数 1
EN

Stack Overflow用户

发布于 2012-04-06 18:48:03

如果您出于某种原因需要自己处理方向更改,一种解决方案是在调用setContentView()之前删除片断。下面是在我的例子中起作用的代码:

代码语言:javascript
复制
    @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
    ...
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9415574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档