我最近在一个看似简单的 Android 布局上苦苦挣扎:我想要一个 WebView
在 Button
之上。它适用于以下参数:
WebView:
Height: wrap-content
Weight: unset (by the way, what is the default?)
Button:
Height: wrap-content
Weight: unset
但是,如果网页变得太大,它就会溢出按钮。我尝试了各种重量和高度的组合,除了一个以外,所有组合要么完全隐藏按钮,要么部分覆盖它。这是有效的(从 http://code.google.com/p/apps-for-android/source/browse/trunk/Samples/WebViewDemo/res/layout/main.xml 复制):
WebView:
Height: 0
Weight: 1
Button:
Height: wrap-content
Weight: unset
如果您更改其中任何一个,例如给按钮一个权重或将 WebView
高度更改为 wrap-content 然后它不起作用。我的问题是:为什么?有人可以解释一下 android 布局系统到底在想什么吗?
最佳答案
像下面这样的东西应该会给你你想要的。关键是 WebView 的 layout_height="fill_parent"和 layout_weight="1"。
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
编辑:糟糕,我误解了你的问题。 layout_weight 使其不会溢出按钮(或您示例中的 textview)。我不确定为什么会发生这种情况,但是如果您的 LinearLayout 中有一个“fill_parent”项目,除了一个或多个“wrap_content”项目之外,您还需要为“fill_parent”项目指定一个 layout_weight,否则它会花费在其余小部件的空间上。
关于android - WebView 和 Button 的 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2519794/