以LinearLayout為例,它提供的background屬性将會将背景圖檔拉伸,相當難看。事實上我們隻需做少量的修改就可以實作web程式設計中css背景圖檔的效果。來試試吧。
A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a BitmapDrawable from a file path, an input stream, through XML inflation, or from a Bitmap object.
屬性 android:tileMode
Defines the tile mode. When the tile mode is enabled, the bitmap is repeated. Gravity is ignored when the tile mode is enabled.
Constant Value Description
disabled -1 Do not tile the bitmap. This is the default value.
clamp 0 Replicates the edge color.
repeat 1 Repeats the bitmap in both direction.
mirror 2 Repeats the shader’s image horizontally and vertically, alternating mirror images so that adjacent images always seam.
在drawable目錄下建立一個repeat_bg.xml:
<code></code>
然後在布局的xml檔案中可以這樣引用: