ImageView: 在 Button 上方适当缩放

3
我是新手Android开发者,一切都进行得很顺利,直到我需要处理布局的时候。 我想要的布局如下: 图片应该正确缩放并保持其比例。
因此,我尝试使用以下XML代码,但它并不按照我想要的方式工作。 图片保持其比例,但是当垂直时,左右两侧超出了屏幕,并在水平时从上部和下部裁剪: enter image description here
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:orientation="vertical"
    android:background="@color/background">


     <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:src="@drawable/logo"
        android:gravity="center"
        android:scaleType="centerCrop"
        android:contentDescription="@string/app_logo"
        android:layout_weight="2.5" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:gravity="center">  

        <Button
            android:id="@+id/id1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:text="@string/text1"
            android:textSize="40sp" 
        />

          <Button
            android:id="@+id/id2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:text="@string/text2"
            />

        <Button
            android:id="@+id/id3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:layout_gravity="center"
            android:text="@string/text3" />
    </LinearLayout>
</LinearLayout>

我搜索了很多但是就是找不到我想要的内容。请帮忙!


1
你能提供一个大致的情况现状和应该是什么样子吗? - g00dy
请分享您期望的输出图片,这样我们就可以弄清楚了。 - Akshay Joy
我在问题中添加了一张图片。 - JustABit
从图像视图中删除 android:layout_weight="2.5",并检查其是否正常工作。 - Abhinav Singh Maurya
不是这样的。然后我必须给图像设置特定的高度,而match_parent和wrap_content都不起作用。 - JustABit
显示剩余3条评论
3个回答

1

看起来你需要的是 android:scaleType="fitCenter"


0

您需要为竖屏状态和横屏状态设计两个布局,并将它们放在 Android 项目的两个不同文件夹中:

当您将常规布局放入布局文件夹时,您应该将横向布局放在 layout-land 文件夹中。


0
尝试将 android:scaleType="fitXY" 设置到你的 ImageView

但它无法保持其比率。 - JustABit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接