我需要在 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/