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

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

问题描述

我的应用程序具有的WebView和一些按钮的LinerLayout内。

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

问题是,我想要的按钮是在底部在纵向模式和留在横向模式,而web视图保持它的状态。

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.

两个不同的布局不工作,因为它迫使该刷新的WebView活动的娱乐。现在我使用机器人:configChanges =定向在活动标记,以便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?

人像模式

横向模式

推荐答案

我测试片段,但是处理的片段,使事情变得更加复杂和片段本身需要保存和恢复它可能无法在web视图具有JavaScript的工作状态,所以,我搜索更多,找到一个不错的文章的地方,并进行一些修改我来到了一个解决方案,我建议:

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:

首先,添加机器人:configChanges =定位|屏幕尺寸|键盘| keyboardHidden这样的应用程序处理配置的变化,而不是机器人

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这样的方法,并把一切都放在这个方法涉及到用户界面的初始化:

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);
}

如果web视图尚不存在将被创建并的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:

and in onConfigurationChanged:

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

在onConfigChange web视图从旧的占位符删除,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天全站免登陆