如何使应用程序的背景图像重复

vir*_*sir 312 layout android image

我在我的应用程序中设置了背景图像,但背景图像很小,我想重复它并填写整个屏幕.我该怎么办?

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:tileMode="repeat">
Run Code Online (Sandbox Code Playgroud)

yan*_*nko 423

好的,这就是我在我的应用程序中得到的内容.它包括一个黑客,以防止ListView滚动时黑色.

drawable/app_background.xml:

<?xml version="1.0" encoding="utf-8"?>
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/actual_pattern_image"
        android:tileMode="repeat" />
Run Code Online (Sandbox Code Playgroud)

values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <style name="app_theme" parent="android:Theme">
    <item name="android:windowBackground">@drawable/app_background</item>
    <item name="android:listViewStyle">@style/TransparentListView</item>
    <item name="android:expandableListViewStyle">@style/TransparentExpandableListView</item>
  </style>

  <style name="TransparentListView" parent="@android:style/Widget.ListView">
    <item name="android:cacheColorHint">@android:color/transparent</item>
  </style>

  <style name="TransparentExpandableListView" parent="@android:style/Widget.ExpandableListView">
    <item name="android:cacheColorHint">@android:color/transparent</item>
  </style>

</resources>
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml:

//
<application android:theme="@style/app_theme">
//
Run Code Online (Sandbox Code Playgroud)

  • 我试过这个,但看到只有单个瓷砖拉伸到所有屏幕:( (2认同)
  • @sabertabatabaeeyazdi您只需要这些文件夹中的图像.XML可以放在`drawable`(withoud` - *dpi`)文件夹中. (2认同)

Las*_*osi 170

可绘制的xml中有一个属性可以执行此操作.机器人:TILEMODE = "回头客"

查看此站点:http: //androidforbeginners.blogspot.com/2010/06/how-to-tile-background-image-in-android.html

  • 我真的不知道这是如此低评价.群体的本能?这是平铺背景的本机实现 (36认同)
  • +1只有一件事必须纠正在文章中错误地提到:`你需要将这个backrepeat.xml文件和相关图像添加到其中每一个以允许高,中和低dpi的这个功能.您只需将引用的drawable放在所有密度桶中.引用XML drawable可以放在`drawable`文件夹中,这就足够了. (6认同)
  • 这个就像一个魅力.这个似乎也是正确的方法. (5认同)
  • 我同意这应该是公认的答案.它非常简单,完美无缺! (3认同)

plo*_*man 67

这是一个重复的背景图像的纯java实现:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.bg_image);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
    bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
    LinearLayout layout = new LinearLayout(this);
    layout.setBackgroundDrawable(bitmapDrawable);
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我们的背景图像必须存储在res/drawable/bg_image.png中.

  • android.graphics.Shader (6认同)
  • 什么是Shader? (4认同)
  • 不推荐使用构造函数BitmapDrawable,这段代码不再适用. (3认同)

小智 14

扩展了plowman的答案,这里是使用java更改背景图像的非弃用版本.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bitmap bmp = BitmapFactory.decodeResource(getResources(),
            R.drawable.texture);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(),bmp);
    bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT,
            Shader.TileMode.REPEAT);
    setBackground(bitmapDrawable);
}
Run Code Online (Sandbox Code Playgroud)