如何在运行时更改webview的高度?

时间:2010-02-26 11:12:38

标签: android resize webview

我有一个webview和一个包含4个按钮的表。我已将webview的高度设置为400px,因此按钮以纵向模式显示在屏幕底部。我的问题是,当更改为横向模式时,按钮不可见。因此,当更改方向时,我需要在运行时更改webview的高度。有谁知道如何实现这一目标?我可以使用'onConfigurationChanged'函数捕获方向更改。


感谢CommonsWare,我给了xml如下。应用程序在启动时崩溃。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<RelativeLayout android:layout_alignParentTop="true">

<WebView
    android:id="@+id/appView"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" />

</RelativeLayout>
<RelativeLayout
    android:layout_alignParentBottom="true">

<TableLayout
    android:id="@+id/TableLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:gravity="center_vertical">

<TableRow
    android:id="@+id/TableRow01" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

<ImageButton
    android:id="@+id/more"
    android:padding="1dip"
    android:src="@drawable/more1"
    android:layout_marginRight="15dip">
</ImageButton>
<ImageButton
    android:id="@+id/albums"
    android:padding="1dip"
    android:src="@drawable/albums1"
    android:layout_marginRight="15dip">
</ImageButton>
<ImageButton
    android:id="@+id/videos"
    android:padding="1dip"
    android:src="@drawable/videos"
    android:layout_marginRight="15dip">
</ImageButton>
<ImageButton
    android:id="@+id/artists"
    android:padding="1dip"
    android:src="@drawable/artists1"
    android:layout_marginRight="15dip">
</ImageButton>
</TableRow>
</TableLayout>
</RelativeLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:4)

设计布局以避免400px的硬连线值。在您正在测试的任何仿真器/设备上,它不仅不能在横向模式下工作,而且不会像您期望的那样在更小或更大的屏幕上工作。

请使用RelativeLayoutandroid:layout_alignParentBottom等属性用于按钮,android:layout_alignParentTopandroid:layout_above用于WebView。然后,您的布局将动态适应可用空间。

相关问题