简体   繁体   English

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

[英]Resize ImageView to make entire layout fill screen

I have a screen that contains some TextViews and an ImageView inside a LinearLayout with vertical orientation. 我有一个屏幕,其中在一些带有垂直方向的LinearLayout中包含一些TextViews和一个ImageView。 I would like the whole thing to fit exactly in the screen (no matter what its size is) where the ImageView is the one that adjusts itself to accommodate this. 我希望整个东西完全适合屏幕(无论大小如何),其中ImageView是可以自行调整以适应此情况的屏幕。

I've seen here a few variations of this question (including this ) but didn't find anything that really answers my requirement. 我在这里看到了这个问题的一些变体(包括this ),但没有找到任何能真正满足我要求的东西。

So far i've used a solution which is not very "pretty", which is putting the entire LinearLayout inside a ScrollView, and use a custom ImageView that on its onMeasure method calculates the delta between the height of the ScrollView to the height of the screen. 到目前为止,我使用的解决方案不是很“漂亮”,它是将整个LinearLayout放在ScrollView中,并使用自定义ImageView,在其onMeasure方法上计算ScrollView的高度与滚动的高度之间的差值。屏幕。 If the former is larger than the latter then the delta is subtracted from the ImageView's measured height. 如果前者大于后者,则从ImageView的测量高度中减去增量。

This solution is not perfect and i would really like to know if there is a better one. 这个解决方案并不完美,我真的很想知道是否有更好的解决方案。 Im sure there is. 我肯定有。

Appreciate your help. 感谢您的帮助。

EDIT : here is the layout xml 编辑 :这是布局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 : and here's a sketch that i hope will make things clearer. EDIT2 :这是我希望可以使事情更清楚的草图。 在此处输入图片说明

The sketch is showing 2 different screen sizes cause this solution would need to support also different devices with different screen sizes. 草图显示了2种不同的屏幕尺寸,因为此解决方案还需要支持具有不同屏幕尺寸的不同设备。

On the ImageView, set android:layout_height="0dp" and android:layout_weight="1" . 在ImageView上,设置android:layout_height="0dp"android:layout_weight="1" This will cause it to fill the remaining space (more about layout_weight here ). 这将导致它填充剩余的空间( 此处更多有关layout_weight的信息 )。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM