android – 如何为ImageView指定动态高度?

前端之家收集整理的这篇文章主要介绍了android – 如何为ImageView指定动态高度?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的显示器顶部有一个 ImageView,如下所示:
╔══════════════════════════════════════════════╗
║ ImageView    ╔══════════════╗                ║
║              ║              ║                ║
║              ║ Actual image ║                ║
║              ║              ║                ║
║              ║              ║                ║
║              ║              ║                ║
║              ╚══════════════╝                ║
╚══════════════════════════════════════════════╝

在这下面,我有一系列的按钮和TextViews ..

我希望ImageView的高度根据屏幕的最大高度动态增加.我正在对齐包含沿着屏幕边缘底部的按钮的布局,我希望上面的ImageView的屏幕的其余部分.

此外,由于ImageView包含一个居中的图像,我也想设置最小高度,如果屏幕太小,不能显示图像视图和下面的按钮,则滚动条.

谢谢!

这可能吗?

解决方法

如果您使用android:layout_width,第一部分应该很简单.如果仅为ImageView(或其容器)设置layout_width,则它将扩展为尽可能多地占用父布局.例如,如果您想要一个ImageView占用整个屏幕的其余部分的布局,您可以这样做:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:src="@drawable/icon"
        android:scaleType="fitCenter" android:layout_weight="1" />
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <!-- Put whatever you want in here -->
    </LinearLayout>
</LinearLayout>

在这个例子中,我正在拉伸图像以占据整个屏幕,但是您也会出现“如果图像对于屏幕太大而需要滚动”的问题.在这种情况下,您需要做的就是将一个ScrollView添加到布局中.如果图像的垂直度太高,屏幕会自动滚动:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:fillViewport="true">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <ImageView android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:src="@drawable/huge"
            android:scaleType="fitCenter" android:layout_weight="1" />
        <LinearLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <!-- Insert your content here -->
        </LinearLayout>
    </LinearLayout>
</ScrollView>

一个重要的事情要注意:如果您在ScrollView上没有将android:fillViewport设置为true,那么太小的ImageViews将不会填满整个屏幕.

原文链接:https://www.f2er.com/android/312494.html

猜你在找的Android相关文章