android - 如何保持 Android 布局中的图像与原始图像成比例?

标签 android xml imageview

我需要在 android 屏幕的顶部放置一个图像(一个标志),我需要它和整个屏幕一样宽,我需要它的高度与宽度成正比,与原始图像。到目前为止,我尝试过的所有操作都会导致图像比例失调或图像覆盖整个屏幕。我该如何解决这个问题?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@color/blue">




    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/linearLayout"
        android:layout_centerVertical="true"
        >

        <EditText
            android:id="@+id/username"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="@dimen/margin_main"
            android:layout_marginRight="@dimen/margin_main"
            android:hint="@string/username"
            android:textColorHint="#fff"
            android:gravity="center"
            android:background="@drawable/input_background"
            android:layout_marginBottom="@dimen/margin_main"
            android:textColor="#fff"
            />

        <EditText
            android:id="@+id/password"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:inputType="textPassword"
            android:layout_marginLeft="@dimen/margin_main"
            android:layout_marginRight="@dimen/margin_main"
            android:hint="@string/password"
            android:textColorHint="#fff"
            android:gravity="center"
            android:background="@drawable/input_background"
            android:layout_marginBottom="@dimen/margin_main"
            android:textColor="#fff"/>


        <Button
            android:id="@+id/login_button"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:text="@string/login"
            android:layout_marginLeft="@dimen/margin_main"
            android:layout_marginRight="@dimen/margin_main"
            android:background="@drawable/button_yellow_background"
            android:textStyle="bold"
            android:textSize="20sp"
            android:textColor="@color/blue"/>

    </LinearLayout>

    <ImageView
        android:id="@+id/logo"
        android:layout_width="315dp"
        android:layout_height="100dp"
        android:background="@drawable/logo"/>


</RelativeLayout>

有问题的图像是 Logo 。

最佳答案

你需要做的就是改变

<ImageView
        android:id="@+id/logo"
        android:layout_width="315dp"
        android:layout_height="100dp"
        android:background="@drawable/logo"/>

<ImageView
        android:id="@+id/logo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/logo"/>

注意:如果您的图像小于屏幕宽度,这样做可能会导致图像拉伸(stretch)。

关于android - 如何保持 Android 布局中的图像与原始图像成比例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28306538/

相关文章:

java - 需要有关同步的帮助

java - 操作栏选项卡必须有回调 addTab 错误

wpf - 强制 XML 有结束标签?

xml - 在带有 not() 的 bash 中使用 XPath

android - 从自定义相机拍摄和保存后如何将图片显示到 Activity 中

android - strings.xml 资源有多大?

python - lxml:获取所有叶节点?

android - 我的程序以编程方式添加 ImageView 有什么问题

android - 如何从 Recycler View 中拖放图像并将其放置到另一个布局中

java - 从其他类调用公共(public)字符串