我有一个LinearLayout,它是ScrollView的直接子级,有两个子级。第一个孩子几乎可以是任何高度(这是一个图像),但是第二个孩子将拥有比视口高度大得多的内容。我尝试使用layout_weight实现此目的,但这仅导致将图像设置为1dp之类的内容,而第二个则由其余的孩子承担,因为权重仅分配给了剩余空间。

如何强制图像占据至少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>

最佳答案

您在android:adjustViewBounds="true"上缺少ImageView

将您的ImageView代码更改为这样的内容

<ImageView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="4"
    android:src="@drawable/my_image"
    android:adjustViewBounds="true"
 />

10-06 13:56
查看更多