【问题标题】:Forcing a view to take at least a percentage of its parent's height强制视图至少占据其父级高度的百分比
【发布时间】:2014-10-21 06:49:30
【问题描述】:

我有一个 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 android-layout android-linearlayout parent-child


    【解决方案1】:

    您的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"
     />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 2017-07-22
      • 1970-01-01
      • 2013-10-12
      • 2013-08-23
      • 1970-01-01
      相关资源
      最近更新 更多