[英]How do I center an image with no stretching on my Android XML layout Splash screen
這是我的代碼。 它將我的圖像拉伸到屏幕的寬度。 我只想讓它居中而不拉伸。 我這是怎么了?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/splash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#FFFFFF"
android:src="@drawable/splash_screenhdpi" />
</RelativeLayout>
您需要設置一個scaleType android:scaleType="center"
我不太了解您的問題,希望這樣:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#FFFFFF"
android:src="@drawable/splash_screenhdpi"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:scaleType="center" />
</RelativeLayout>
您必須對ImageView
使用屬性scale
。 使用center只是將圖像以圖像的默認大小居中,而match_parent
作為寬度和高度的大小。
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/ic_launcher"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.