layout-land xml 文件不适用于 onConfigurationChanged 回调

发布于 2024-10-22 06:58:16 字数 187 浏览 6 评论 0原文

我有不同的纵向和横向模式布局,并且还需要重写 onConfigurationChanged() 回调。但问题是,当我将手机方向更改为横向时,我的横向布局不起作用。

谁能告诉我这是 onConfigurationChanged 回调问题还是其他原因导致的?

任何帮助将不胜感激。

I have different layouts for portrait and landscape mode and I also need to override the onConfigurationChanged() callback. But problem is when I change the phone orientation to landscape my landscape layout does not work.

Can anybody tell me is this onConfigurationChanged call back problem or something else causing that?

Any help will be appreciative.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩物 2024-10-29 06:58:16

我还需要重写 onConfigurationChanged() 回调

为什么?

但问题是当我将手机方向更改为横向时,我的横向布局不起作用。

我猜测“不起作用”意味着景观布局没有生效。鉴于您所做的事情,这是预期的行为。

要解决此问题,最好删除 android:configChanges="keyboardHidden|orientation"。放入该属性应作为最后的手段,通常适用于没有单独的纵向与横向布局文件的活动。

i also need to override the onConfigurationChanged() callback

Why?

but problem is when i change the phone orientation to landscape my landscape layout does not work.

I am going to guess that "does not work" means that the landscape layout does not take effect. This is expected behavior given what you have done.

To resolve this problem, ideally you delete android:configChanges="keyboardHidden|orientation". Putting in that attribute should be done as a last resort, and typically for activities that do not have separate portrait versus landscape layout files.

风和你 2024-10-29 06:58:16

我相信它绝对会对您有所帮助...

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
    case Configuration.ORIENTATION_LANDSCAPE:
        setContentView(R.layout.main_land);
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        setContentView(R.layout.main);
        break;
    }
    Toast.makeText(this, "Helloo", Toast.LENGTH_SHORT).show();
}
enter code here
@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);

    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
    case Configuration.ORIENTATION_LANDSCAPE:
        setContentView(R.layout.main_land);
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        setContentView(R.layout.main);
        break;
    }
}

@Override
public Object onRetainNonConfigurationInstance() {
    // TODO Auto-generated method stub
    return super.onRetainNonConfigurationInstance();
}

}

并将此行添加到您的清单文件中..
android:configChanges="keyboardHidden|方向"

I am sure that it will definitively help you...

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
    case Configuration.ORIENTATION_LANDSCAPE:
        setContentView(R.layout.main_land);
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        setContentView(R.layout.main);
        break;
    }
    Toast.makeText(this, "Helloo", Toast.LENGTH_SHORT).show();
}
enter code here
@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);

    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
    case Configuration.ORIENTATION_LANDSCAPE:
        setContentView(R.layout.main_land);
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        setContentView(R.layout.main);
        break;
    }
}

@Override
public Object onRetainNonConfigurationInstance() {
    // TODO Auto-generated method stub
    return super.onRetainNonConfigurationInstance();
}

}

and add this line in your manifest file..
android:configChanges="keyboardHidden|orientation"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文