Rodrigo Asked: 2020-04-07 07:57:09 +0800 CST 2020-04-07 07:57:09 +0800 CST 2020-04-07 07:57:09 +0800 CST 在布局中定义 WebView 大小 772 如何在我的 xml 中定义 Web 视图的大小?我的应用程序必须在 web 视图中显示一个站点,但它不能占据屏幕的整个高度,因为必须添加 MediaPlayer 控件。我不想在 px 中定义它,因为显然屏幕的大小是多种多样的。 android 2 Answers Voted Best Answer Jorgesys 2020-04-07T08:07:58+08:002020-04-07T08:07:58+08:00 为了始终保持屏幕大小的百分比,选项是layout_weight在您的视图中定义属性,假设以下示例,其中我们需要WebView始终占据屏幕的 50% 并位于底部,但是我们还需要添加另外 2 个视图,其中一个占屏幕的 30%,第二个占 20%。 如果我们将屏幕的总数考虑为 1 ,我们分配一个 0.50 的值,以便它始终占据我们屏幕的 50% WebView: <WebView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".50" android:id="@+id/webView" android:layout_alignParentBottom="true" /> 因此,对于其他视图,一个将占据 30%(我们分配 0.30)和另一个 20%(我们分配 0.20): <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".30" android:text="Vista 1" /> <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".20" android:text="Vista 2" /> <WebView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".50" android:id="@+id/webView" android:layout_alignParentBottom="true" /> </LinearLayout> 我们可以确保视图总是在屏幕上测量指定的百分比,定义android:layout_height="0dp". 另一个例子: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <WebView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.85" android:id="@+id/webView" android:layout_alignParentBottom="true" /> <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".15" android:text="Vista 2" android:visibility="visible" /> </LinearLayout> tinoper 2020-04-07T10:32:42+08:002020-04-07T10:32:42+08:00 使用 LinearLayout 时,您可以使用 weight 属性。(http://developer.android.com/intl/es/guide/topics/ui/layout/linear.html) 也许这允许您通过为 webview 和控件设置不同的权重,在不忽略控件的情况下利用屏幕显示内容。 (另一个解释链接,但在西班牙语中,它不是我的,但它有助于澄清问题,http://androideity.com/2012/06/01/ui-fluidas-y-la-propiedad-weight-en-安卓/)
为了始终保持屏幕大小的百分比,选项是
layout_weight
在您的视图中定义属性,假设以下示例,其中我们需要WebView
始终占据屏幕的 50% 并位于底部,但是我们还需要添加另外 2 个视图,其中一个占屏幕的 30%,第二个占 20%。如果我们将屏幕的总数考虑为 1 ,我们分配一个 0.50 的值,以便它始终占据我们屏幕的 50%
WebView
:因此,对于其他视图,一个将占据 30%(我们分配 0.30)和另一个 20%(我们分配 0.20):
我们可以确保视图总是在屏幕上测量指定的百分比,定义
android:layout_height="0dp"
.另一个例子:
使用 LinearLayout 时,您可以使用 weight 属性。(http://developer.android.com/intl/es/guide/topics/ui/layout/linear.html)
也许这允许您通过为 webview 和控件设置不同的权重,在不忽略控件的情况下利用屏幕显示内容。
(另一个解释链接,但在西班牙语中,它不是我的,但它有助于澄清问题,http://androideity.com/2012/06/01/ui-fluidas-y-la-propiedad-weight-en-安卓/)