如何在 Android 中的给定布局内调整 ImageView 的大小?

How to resize an ImageView within a given Layout in Android?

我无法针对特殊情况进行适当的布局。我已经在设计器和代码中对此进行了一段时间的试验,但我找不到解决方案,这就是为什么我需要你的帮助。

我必须创建一个布局,其结构应如下图所示。主要是几个linearLayout的组合。我遇到的问题是,图片只能在代码中添加,因为此布局是显示列表中项目信息的详细视图。

问题: 如何获得看起来像底部图片的布局(在代码中加载图像后)?图像在代码中加载后,必须自行调整大小,因此它使用整个可用高度并相应地调整其宽度。 "relativeLayout_Top" 和“linearLayout_BelowImage”都有固定的高度。 “scrollView_BigRight”根据 space 进行自我调整,而“imageView_OrangeImage”不会需要自己。

我可以处理在添加图像后调整代码布局的解决方案,或者使 layout.xml 本身足够灵活以处理这种情况的解决方案。

非常感谢任何帮助。如果您需要更多信息,请告诉我。

下面是我的layout.xml的主要内容,需要解决这个问题

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/white">

    <RelativeLayout
        android:id="@+id/relativeLayout_Top"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/blue" >
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linearLayout_Big"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:background="@color/transparent" >

        <LinearLayout
            android:id="@+id/LinearLayout_BigLeft"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@color/transparent" >

            <ImageView
                android:id="@+id/imageView_OrangeImage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@color/black" />

            <LinearLayout
                android:id="@+id/linearLayout_BelowImage"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@color/blue_white_blue" >

                <Button
                    android:id="@+id/btn1"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@color/blue" />

                <TextView
                    android:id="@+id/textView_BelowImageMiddle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@color/white" />

                <Button
                    android:id="@+id/btn2"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@color/blue" />
            </LinearLayout>
        </LinearLayout>

        <ScrollView
            android:id="@+id/scrollView_BigRight"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/grey" >
        </ScrollView>
    </LinearLayout>
</LinearLayout>

android:adjustViewBounds="true"

This one’s a manual fix for “optimized” code in scaleType="fitCenter". Basically when Android adds an image resource to the ImageView it tends to get the width & height from the resource instead of the layout. This can cause layouts to reposition around the full size of the image instead of the actual viewable size.

AdjustViewBounds forces Android to resize the ImageView to match the resized image prior to laying everything else out. There are times where this calculation won’t work, such as when the ImageView is set to layout_width="0dip". If it’s not working, wrap the ImageView in a RelativeLayout or FrameLayout which handles the 0dip flexible size instead

get it from this site

模式android:scaleType="centerCrop"均匀拉伸图像以填充整个容器并进行不必要的修剪。

You can change the way it default scales images using the android:scaleType parameter. By the way, the easiest way to discover how this works would simply have been to experiment a bit yourself!

get it here