Android-如何在不缩放的情况下添加背景?
我想为我的应用程序添加背景。我尝试了两种方法Android-如何在不缩放的情况下添加背景?,android,background,Android,Background,我想为我的应用程序添加背景。我尝试了两种方法 <ImageView android:contentDescription="@string/background_description" android:gravity="center" android:id="@+id/imageView1" android:layout_width="fill_parent" android:lay
<ImageView
android:contentDescription="@string/background_description"
android:gravity="center"
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/bg"/>
到我的XML文件,也使用android:background,但两者都有以下结果:
我希望它看起来像这样:
另外,在不同的屏幕密度/分辨率下,我必须做些什么才能使它看起来像这样?如果您希望它缩放,但保持其宽度/高度比,请使用:
android:scaleType="fitCenter"
要获得以下信息:
<ImageView
android:contentDescription="@string/background_description"
android:gravity="center"
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="@drawable/bg"/>
您可以在中获取更多信息。
此外,您不应该对任何布局宽度或布局高度使用
“填充父项”
,因为它已被弃用。相反,您应该使用“match_parent”
,如上面的代码所示。如果您将这一行添加到xml格式的imageview中
android:scaleType="fitXY"
它将缩放您正在使用的图像,以精确匹配imageview
对于不同的屏幕分辨率,用相同的图像填充不同的可绘制文件夹,只需使用不同设备所需的分辨率即可您也可以这样做:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background" >
否则:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_cntent" >
同时将*.png文件移动到drawable nodpi文件夹以获得最佳效果
将看起来像:
药片:
移动电话:
我添加了android:scaleType=“fitCenter”,并将fill\u parent更改为match\u parent,但仍然是一样的!