调整ImageView的大小以使整个布局填充屏幕

时间:2014-12-14 19:22:46

标签: android android-layout

我的屏幕包含一些TextViews和一个垂直方向的LinearLayout内的ImageView。我希望整个事情完全适合屏幕(无论它的大小),ImageView是调整自身以适应这种情况的。

我在这里看到了这个问题的一些变体(包括this),但没有找到真正满足我要求的任何内容。

到目前为止,我已经使用了一个不是很漂亮的解决方案,它将整个LinearLayout放在ScrollView中,并在其 onMeasure上使用自定义ImageView 方法计算ScrollView的高度与屏幕高度之间的差值。如果前者大于后者,则从ImageView的测量高度中减去delta。

这个解决方案并不完美,我真的很想知道是否有更好的解决方案。我确定有。

感谢您的帮助。

编辑:这是布局xml

<com.xxx.widgets.LockableScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.venews"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    custom:scrollable="false"
    android:fillViewport="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/login_horizontal_margin"
    android:paddingRight="@dimen/login_horizontal_margin"
    android:orientation="vertical">

    <com.xxx.widgets.ResizableToFitScreenImageView android:id="@+id/login_logo_image"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/login_logo_margin"
        android:layout_marginBottom="@dimen/login_logo_margin"
        android:src="@drawable/ic_logo_and_name"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView android:id="@+id/login_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"/>

        <TextView android:id="@+id/login_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/login_username"/>

        (...Other stuff...)

    </RelativeLayout>

</LinearLayout>

</com.xxx.widgets.LockableScrollView>

EDIT2 :这是一个草图,希望能让事情变得更清晰。 enter image description here

草图显示了2种不同的屏幕尺寸,因此该解决方案还需要支持不同屏幕尺寸的不同设备。

1 个答案:

答案 0 :(得分:1)

在ImageView上,设置android:layout_height="0dp"android:layout_weight="1"。这将使其填充剩余空间(更多关于layout_weight here)。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
相关问题