android 旋转器的屏幕方向处理

发布于 2024-12-26 20:41:41 字数 386 浏览 4 评论 0原文

我有一个活动,其中有一个旋转器。因为对于纵向和横向模式,我有不同的布局,所以我在 onConfigurationChanged 方法中更改布局

@Override
    public void onConfigurationChanged(Configuration conf) {
        super.onConfigurationChanged(conf);
        setContentView(R.layout.layout);
        initUI();
    } 

,但问题是当我更改方向时,我的微调器会重新创建,因此如果微调器在纵向模式下打开,它会接近横向模式。我的要求是:如果在任何模式下打开,都应该在方向改变后打开。请让我知道如何处理这种情况。

I have a activity in which there is a spinner. since for portrait and landscape mode i have different layout so I am changing layout in onConfigurationChanged method

@Override
    public void onConfigurationChanged(Configuration conf) {
        super.onConfigurationChanged(conf);
        setContentView(R.layout.layout);
        initUI();
    } 

but the problem is when I change orientation , my spinner is recreated so if spinner is open in portrait mode it get close in landscape mode.My requirement is : if it is open in any mode , it should be open after orientation change.can you please let me know how to handle this situation.

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

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

发布评论

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

评论(3

情独悲 2025-01-02 20:41:41

尝试 spinner 的 PerformClick() 方法

try spinner's performClick() method

也只是曾经 2025-01-02 20:41:41

要停止重新创建 Spinner,您可以将其添加到清单文件中,

<activity android:name=".Activity_name"
          android:configChanges="orientation|keyboardHidden">

但是通过添加此内容,当您旋转设备时,您的布局将不会自动更改,因此您必须像这样手动管理它,

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            setContentView(R.layout.login_landscape);
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.login);         
        }
    }

有关更多信息,您可以查看我的回答这里

To stop re-creation of your Spinner you can add this in your Manifest file

<activity android:name=".Activity_name"
          android:configChanges="orientation|keyboardHidden">

But by adding this your Layout will not be changed automatically when you rotate your device, so you have to manage that manually like this,

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            setContentView(R.layout.login_landscape);
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.login);         
        }
    }

For more information you can check my answer here.

椵侞 2025-01-02 20:41:41

一旦方向改变,销毁方法就会被调用,你的活动会再次重新创建。为了避免调用 destroy 方法,您需要在清单文件中添加以下代码。但在这种情况下只能使用一种布局,如果您想重新发布内容,则需要动态执行此操作。

android:configChanges="orientation|keyboardHidden"

Once the orientation changes, destroy method get called and your activity re-create once again. To avoid destroy method get called you need to add below codes in manifest file. But in this case only one layout can be used, if you want to repostion your contents you need to do it dynamically then.

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