更换方向更改时的布局

时间:2012-12-12 20:57:53

标签: android android-layout

我的应用在LinerLayout中有一个webview和一些按钮。

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

两种不同的布局不起作用,因为它强制重新创建刷新webview的活动。现在我在activity标签中使用 android:configChanges =“orientation”,这样webview就不会在方向更改时刷新。

是否有更改屏幕模式更改按钮的布局?

enter image description here

肖像模式

enter image description here

横向模式

6 个答案:

答案 0 :(得分:13)

我测试了片段,但处理片段会使事情变得更加复杂,片段本身需要保存和恢复,这可能不适用于具有javascript状态的webview,所以我搜索了更多并在某处找到了一篇好文章并进行了一些修改我找到了一个解决方案,我建议:

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

为横向和纵向制作两种不同的布局。在两个布局而不是webview中放置一个FrameLayout,它充当webview的占位符。

像这样定义initUI方法,并将与UI初始化相关的所有内容放在此方法中:

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自定义。

onConfigurationChanged

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

onConfigChange中,将从旧占位符中删除网页视图,并调用initui,这会将其添加回新版面。

oncreate()致电initui(),以便ui首次初始化。

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

我希望对某人有所帮助。

答案 1 :(得分:5)

layout-land放在您希望作为横向的布局上。

这个想法是,你不应该真正使用configChange="orientation"因为它有它的缺点。您可以找到详细的帖子here。如果要更改布局,则应手动处理状态。当然,您可以编程执行此操作但是如果您想使用xml来执行此操作。您可以使用片段维护webView。

答案 2 :(得分:5)

如果您使用android:configChanges="orientation|screenSize"

在清单中,它忽略了“ layout-land ”中的XML。如果您为横向创建不同的XML,请不要在清单中为该活动使用android:configChanges="orientation|screenSize"标记。

答案 3 :(得分:4)

尝试使用此代码:

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

答案 4 :(得分:1)

使您的视图成为相对布局。当方向改变时,只需调整代码中每个视图的布局参数。 让您的按钮包含在线性布局中

如...

<强>肖像

LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
buttonLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
buttonLinearLayout.setLayoutParams(lp);


LayoutParams webParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.ABOVE, R.id.buttons);
webView.setLayoutParams(webParams);

<强>风景

LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
buttonLinearLayout.setOrientation(LinearLayout.VERTICAL);
buttonLinearLayout.setLayoutParams(lp);


LayoutParams webParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.TO_RIGHT_OF, R.id.buttons);
webView.setLayoutParams(webParams);

确保您使用的LayoutParams是RelativeLayout参数(始终使用视图父母的Layoutparams)

答案 5 :(得分:-1)

您可以在资源下添加值 - (您需要的配置 - 例如,land,portrait)文件夹,并提及此配置所需的布局。就这么简单。

请查看以下链接以获取更多信息 - http://developer.android.com/training/multiscreen/screensizes.html#TaskUseAliasFilters