How to add a vertical-tile (only) background to an Android app
It's very simple to repeat horizontally and vertically the background of a linear layout in a Android's app. You simply need to use tileMode (see How to make android app's background image repeat)
However tileMode in xml file doesn't allow me to repeat the background image only in vertical axis. There's a tileModeY but you cannot access it from xml.
I'm trying to modify that property to "repeat".
I've a main.xml file with the linear layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainMenuLayout"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
I've a image in drawable/welcomebackground.png and also a xml file for the image in drawable/welcomebackgroundpattern.xml:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
an开发者_运维百科droid:src="@drawable/welcomebackground"
android:dither="true" />
In my activity I've tried this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
BitmapDrawable background = (BitmapDrawable) res.getDrawable(R.drawable.welcomebackground);
background.setTileModeY(Shader.TileMode.REPEAT);
(new View(this)).setBackgroundResource(R.drawable.welcomebackground);
}
But my background is still all black. Of course I'm able to add it from the xml but it's repeated in both x and y axis.
So my question is quite simple: how can I repeat vertically "welcomebackground" image in my activity?
I've found the solution: Android Bitmap Tile on X Only
精彩评论