我确实有一个布局,其中包含一个 WebView 和一个位于顶部的图库。我想总是给画廊 20% 的屏幕。 WebView 应该总是占据 80%。
通常这适用于我所有布局中的 layout_weight,其中较高的值是较小的 View ,1 是最大的 View 。在这个布局中它是完全不同的。在这里,我需要为更大的元素使用更高的值,并且它不会保持固定比率。
非常感谢任何帮助。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical" >
<Gallery
android:id="@+id/gallery"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="fill_parent"
android:spacing="2dip" />
<View
android:background="@android:color/white"
android:layout_height="1dip"
android:layout_marginTop="6dip"
android:layout_width="fill_parent" />
<WebView
android:id="@+id/webview"
android:layout_height="wrap_content"
android:layout_weight="5"
android:layout_width="fill_parent" />
</LinearLayout>
最佳答案
权重用于在考虑任何高度/宽度值设置后分配额外空间。对于垂直的 LinearLayout,如果要确保 20/80 的比例(忽略分隔符),则应将组件的高度设置为零(与哪个单位无关)。否则,Android 将首先考虑它们的高度,然后仅按照权重的比例分配剩余空间。
例如,考虑有一个宽度为 100 像素的(水平 LinearLayout)容器,以及两个权重均为 1,但宽度分别设置为 10 像素和 30 像素的组件。为了计算它们的大小,我们首先从容器大小中减去宽度:100 - 10 - 30 = 60。然后我们将其按权重比例划分:60/(1+1) = 30,并相应地分配每个部分。第一个 View 的宽度为 10+30 = 40,而第二个 View 的宽度为 30+30 = 60,即使它们具有相同的权重。
试试下面的布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical" >
<Gallery
android:id="@+id/gallery"
android:layout_height="0px"
android:layout_weight="1"
android:layout_width="fill_parent"
android:spacing="2dip" />
<View
android:background="@android:color/white"
android:layout_height="1dip"
android:layout_marginTop="6dip"
android:layout_width="fill_parent" />
<WebView
android:id="@+id/webview"
android:layout_height="0px"
android:layout_weight="5"
android:layout_width="fill_parent" />
</LinearLayout>
关于android - 如何在 Gallery 和 WebView 之间按固定比例布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6965665/