我有一个 LinearLayout,它是 ScrollView 的直接子级,有两个子级。第一个 child 几乎可以是任何高度(它是一个图像),但第二个 child 的内容将比视口(viewport)的高度大得多。我尝试使用 layout_weight 来实现这一点,但这只会导致图像被设置为 1dp 之类的东西,而第二个 child 则负责其余部分,因为权重仅分配给剩余空间。
如何强制图像至少占据屏幕空间的 40%?
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:src="@drawable/my_image" />
<LinearLayout
android:id="@+id/prediction_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@android:color/white"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="[really long text]" />
</LinearLayout>
</LinearLayout>
</ScrollView>
最佳答案
您的 ImageView
上缺少 android:adjustViewBounds="true"
将你的 ImageView
代码改成这样
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:src="@drawable/my_image"
android:adjustViewBounds="true"
/>
关于android - 强制 View 至少占据其父级高度的一定百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26480547/