在方向改变时替换布局 [英] replace layout on orientation change

查看:24
本文介绍了在方向改变时替换布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在 LinerLayout 中有一个 webview 和一些按钮.

My app has a webview and some buttons inside a LinerLayout.

问题是,我希望按钮在纵向模式下位于底部,在横向模式下位于左侧,而 webview 保持其状态.

the problem is, I want the buttons to be on bottom in portrait mode and on left in landscape mode while the webview maintains it's state.

两种不同的布局不起作用,因为它强制重新创建刷新 web 视图的活动.现在我在活动标签中使用 android:configChanges="orientation" 以便 webview 不会在方向更改时刷新.

Two different layout doesn't work as it force recreation of the activity that refresh the webview. for now I use android:configChanges="orientation" in activity tag so webview doesn't get refreshed on orientation change.

有没有办法在改变屏幕模式时更换按钮的布局?

Is there anyway to replace the layout of buttons on the change of screen mode?

人像模式

横向模式

推荐答案

我测试了 Fragment,但是处理 Fragment 使事情变得更加复杂,并且 Fragment 本身需要保存和恢复,这在具有 javascript 状态的 webview 中可能不起作用,因此,我进行了更多搜索,并在某处找到了一篇不错的文章,经过一些修改,我找到了一个我建议的解决方案:

I tested fragments, but dealing with fragment makes things much more complex and the fragment itself needs saving and restoring which may not work in a webview which has javascript state, So I searched more and find a nice article somewhere and with some modification I came to a solution which I suggest:

首先,添加 android:configChanges="orientation|screenSize|keyboard|keyboardHidden" 以便应用处理配置更改而不是 android.

First, add android:configChanges="orientation|screenSize|keyboard|keyboardHidden" so the app handles the config changes instead of android.

为横向和纵向制作两种不同的布局.在这两个布局而不是 webview 中放置一个 FrameLayout 作为 webview 的占位符.

Make two different layout for landscape and portrait. In both layouts instead of webview place a FrameLayout which acts as a placeholder for the webview.

像这样定义initUI方法,并将所有与UI初始化相关的东西都放在这个方法中:

Define initUI method like this and put everything related to UI initialization in this method:

public void initui()
{
    setContentView(R.layout.main);
    if (wv == null) wv = new WebView(this);
    ((LinearLayout)findViewById(R.id.webviewPlace)).addView(wv);
    findViewById(R.id.home).setOnClickListener(this);
}

如果 webview 尚不存在,它将被创建,并在 setContentView(R.layout.main) 之后将其添加到布局中.您需要的任何其他 UI 自定义都在此之后.

If the webview doesn't exist yet it will be created and after setContentView(R.layout.main) it will be added to the layout. Any other UI customization you need came after this.

并且在 onConfigurationChanged 中:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    ((LinearLayout)findViewById(R.id.webviewPlace)).removeAllViews();
    super.onConfigurationChanged(newConfig);
    initUI();
}

onConfigChange 中,webview 从旧的占位符中移除,initui 将被调用,这将把它添加回新的布局.

In onConfigChange the webview is removed from old placeholder and initui will be called which will add it back to the new layout.

oncreate() 中调用 initui() 这样 ui 将被第一次初始化.

In oncreate() call initui() so the ui will be initialized for the first time.

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    initUI()
}

我希望它对某人有帮助.

I wish it would be helpful for someone.

这篇关于在方向改变时替换布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆