Today, I was working with a layout that I wanted to have a background image repeat throughout the width and height of a Linear Layout.
My First Attempt
Initially it seemed to make sense to create a new layout, place the image into the drawable, resource directory, and reference this as the android:background attribute of the LinearLayout definition
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/honeycomb"/>
As you can see from the screenshot that what I had assumed did not automatically tile or repeat the background, rather it stretched the image the full dimensions of the container.
Second Attempt
After some digging on the internet. I found that you can create a drawable in xml, using a resource of an image drawable, in my case the honeycomb. In the drawable you can tell it that it should tile and repeat as needed.
honeycomb_tile.xml:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/honeycomb"
android:tileMode="repeat" />
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/honeycomb_tile"/>
Results:
No comments:
Post a Comment