在我的应用程序中我将使用很多布局“边框”,这是一个简单的圆角矩形。为了测试它的外观,我创建了一个透明背景的.png文件:
我只是将图像设置为布局背景。问题是我需要不同比例的其他布局,这将浪费时间在Photoshop中为我的所有布局创建这样的图像。
问题:如何使用android API(也许是XML形状)做同样的事情,但是允许矩形拉伸到任何尺寸并仍保持清晰?
答案 0 :(得分:36)
您可以使用drawable
在可绘制文件夹中有bkg.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFFF"/>
<stroke
android:width="3dp"
android:color="#0FECFF"/>
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp"/>
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
然后设置背景
android:background="@drawable/bkg" //to required view
在将可绘制背景设置为textview时,Snasp拍摄了图形编辑器。
答案 1 :(得分:5)
Shape drawable应该这样做。例如:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp" />
<solid android:color="@android:color/transparent" />
<stroke android:width="2dp" android:color="#0000ff" />
</shape>