Android的布局背景质感的xml [英] Android layout background xml texture
问题描述
我搜索了很多,但没有发现任何东西。我想为布局背景,但不是用一种颜色或渐变填充它,我想与纹理填充它,并重复了。我试过,但没有奏效。你能告诉我一个解决方案。
I searched a lot but didn't find anything. I want to make a background for a layout , but instead of filling it with a color or a gradient I want to fill it with a texture and repeat it . I tried this but it didn't work . Can you tell me a solution.
card_texture.xml
card_texture.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/paper_texture"
android:tileMode="repeat"
/>
card_shape.xml
card_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color = "@android:color/transparent"/>
<stroke android:width="2dp"
android:color="@color/text_color"/>
<corners
android:radius="20dp"
/>
</shape>
card_background.xml
card_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/card_texture" >
<corners
android:radius = "20dp"/>
</item>
<item android:drawable="@drawable/card_shape" />
</layer-list>
这就是我如何使用它在我的布局的android:背景=@绘制/ card_background
我得到这样的事情。但我想角落的纹理(用红色标出)是透明的,以适合黑色边框
I get something like this. But I want the corners(marked with red) of the texture to be transparent and to fit in the black border
推荐答案
我称之为 RES /绘制/ bg_tile.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/tile"
android:tileMode="mirror"
android:dither="true"
android:antialias="true"
/>
我用它喜欢的:
android:background="@drawable/bg_tile"
Obiously,我prepared一瓦水平翻转和垂直方向(最瓷砖作出这样的一个有趣的效果),并把它称为 RES /绘-A-dpi的分辨率/ tile.png
A-dpi的分辨率是LDPI,MDPI,...,xxxhdpi
a-dpi-resolution being ldpi, mdpi, ..., xxxhdpi
如果你想使用的复合背景(即梯度和在它平铺位 - 但位图有以某种方式透明,否则会覆盖梯度),你可以做一个LayerList背景(所以不同层是DRAN 1 ontop的另一个)
If you want to use a composite background (i.e. a gradient and a tiling bitmap over it - but the bitmap has to be somehow transparent, or it will cover the gradient), you can make a LayerList background (so that different "layers" are dran one ontop of another)
要做到这一点,你需要添加其他的背景文件,即: RES /绘制/ bg_radial.xml
To do that, you'll need to add another background file, i.e.: res/drawable/bg_radial.xml:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:startColor="#@color/bar_int_md"
android:centerColor="#@color/bar_mid_md"
android:endColor="#@color/bar_ext_md"
android:gradientRadius="480"
android:type="radial"
/>
</shape>
终于LayerList,这粘在一起( RES /绘制/ bg.xml )
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_radial" />
<item android:drawable="@drawable/bg_tile" />
</layer-list>
请注意,第一项被首先绘制。下一个数据项都绘制ontop彼此的,是最后一个最
Note that the first item is drawn first. Next items are drawn ontop of each other, being the last one the "uppermost".
所以,最后你会使用这个像:
So, finally you would use this like:
android:background="@drawable/bg"
不错!
这篇关于Android的布局背景质感的xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!