XML 布局显示的背景图片不正确
Incorrect background image being shown by XML Layout
我创建了一个 XML 的 relativelayout,里面有一个 webview 和一个 relativelayout。我所做的是使用 relativeLayout1 将我的应用程序加载到 webview 及其下方的应用程序徽标上。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/urlContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/relativeLayout1" />
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="@drawable/footerl" >
</RelativeLayout>
</RelativeLayout>
但是发生的事情是图像在小型设备上的纵向图像是正确的,但在较大的设备上或在横向模式下(一旦我旋转它)就会变形。所以我所做的是在 /res 文件夹中添加了不同尺寸的图像:
res/drawable-hdpi/footer.png
res/drawable-ldpi/footer.png
res/drawable-xdpi/footer.png
res/drawable-xxdpi/footer.png
每个图像都有不同的大小,使用每个设备的最大宽度(320px、480px、780px、1200px)
我也使用了不同的布局:
layout/activity_web.xml
layout-large/activity_web.xml
layout-xlarge/activity_web.xml
layout-xlarge-land/activity_web.xml
下面的标志还是变形了。除了处于横向模式的 layout-xlarge-land 之外,所有布局都处于纵向模式。我应该为布局和大布局创建横向模式吗?
更新
我通过添加图像视图来尝试 Darnmason 的回答,看看它是否可以工作,即使它被旋转到横向模式:
但发生的情况是图像显示,但它的宽度类似于纵向模式。我有没有漏掉为什么它没有拉伸?
使用图像作为背景并不能使您更好地控制图像缩放。我建议使用 ImageView
设置
android:src="@drawable/footer"
android:scaleType="centerCrop"
也许您需要 android:adjustViewBounds="true"
这将确保图片充满屏幕,并根据设备的纵横比根据需要进行裁剪。拥有纵向和横向版本仍然有意义,但您不必关心设备之间纵横比的细微差异。有些屏幕比其他屏幕宽!
我创建了一个 XML 的 relativelayout,里面有一个 webview 和一个 relativelayout。我所做的是使用 relativeLayout1 将我的应用程序加载到 webview 及其下方的应用程序徽标上。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/urlContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/relativeLayout1" />
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="@drawable/footerl" >
</RelativeLayout>
</RelativeLayout>
但是发生的事情是图像在小型设备上的纵向图像是正确的,但在较大的设备上或在横向模式下(一旦我旋转它)就会变形。所以我所做的是在 /res 文件夹中添加了不同尺寸的图像:
res/drawable-hdpi/footer.png
res/drawable-ldpi/footer.png
res/drawable-xdpi/footer.png
res/drawable-xxdpi/footer.png
每个图像都有不同的大小,使用每个设备的最大宽度(320px、480px、780px、1200px)
我也使用了不同的布局:
layout/activity_web.xml
layout-large/activity_web.xml
layout-xlarge/activity_web.xml
layout-xlarge-land/activity_web.xml
下面的标志还是变形了。除了处于横向模式的 layout-xlarge-land 之外,所有布局都处于纵向模式。我应该为布局和大布局创建横向模式吗?
更新
我通过添加图像视图来尝试 Darnmason 的回答,看看它是否可以工作,即使它被旋转到横向模式:
但发生的情况是图像显示,但它的宽度类似于纵向模式。我有没有漏掉为什么它没有拉伸?
使用图像作为背景并不能使您更好地控制图像缩放。我建议使用 ImageView
设置
android:src="@drawable/footer"
android:scaleType="centerCrop"
也许您需要 android:adjustViewBounds="true"
这将确保图片充满屏幕,并根据设备的纵横比根据需要进行裁剪。拥有纵向和横向版本仍然有意义,但您不必关心设备之间纵横比的细微差异。有些屏幕比其他屏幕宽!