【问题标题】:Android background as drawable : does not repeatAndroid背景作为可绘制:不重复
【发布时间】:2013-11-25 14:45:30
【问题描述】:

我正在尝试从图像文件制作背景。

我的 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。他们都没有改变这种情况。

【问题讨论】:

  • 如果我的回答对您有所帮助,请将其设置为正确,因为它会对我有所帮助。谢谢。

标签: android xml background repeat tile


【解决方案1】:

这是一个已知的错误。我自己也遇到过,你的代码没有错。

基本上我认为你必须在代码中设置重复而不是在 XML 上。

检查this的答案。

【讨论】:

    【解决方案2】:

    在您的 Java 代码中,在具有属性 android:background 的布局元素上调用此函数,这将确保背景可绘制对象是平铺的。

    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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 2014-07-04
      相关资源
      最近更新 更多