我正在尝试从图像文件中创建背景。
我的 background.xml 文件是:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/bg_pat_100_100"
android:tileMode="repeat"
android:dither="true"
/>
我在布局中使用了这个背景,如下所示:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".XYZ"
android:background="@drawable/background"
>
<!-- ... layout content ... -->
</RelativeLayout>
现在,如果我在模拟器设备中运行它,它会使背景重复,正如预期的那样。
但是如果我在一个真实的设备中运行它,背景就像一个图像,在整个屏幕上拉伸。
可能是什么问题?
我尝试删除应用程序,清除eclipse项目,删除R.java。他们都没有改变这种情况。
答案 0 :(得分:2)
答案 1 :(得分:1)
在Java代码中,在具有属性android:background
的布局元素上调用此函数,这将确保背景drawable是平铺的。
public static void fixBackgroundRepeat(View view) {
Drawable bg = view.getBackground();
if (bg != null) {
if (bg instanceof BitmapDrawable) {
BitmapDrawable bmp = (BitmapDrawable) bg;
bmp.mutate(); // make sure that we aren't sharing state anymore
bmp.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
}
}
}
示例:
fixBackgroundRepeat(findViewById(R.id.my_xml_tag_that_has_background));