所以我想要一个 TextView 周围有一个很酷的边框。我找不到任何标准的方法,所以我想出了这个:
@drawable/custom_bg_1:蓝色圆形
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#439CC8"/>
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
@drawable/custom_bg_2:一个白色的圆形
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
myactivity.xml:活动的 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:orientation="vertical"
android:padding="15dp" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="2dp"
android:background="@drawable/custom_bg_1" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/curr_loc"
android:textSize="15sp"
android:background="@drawable/custom_bg_2" />
</LinearLayout>
</Linearlayout>
结果:
我在这里所做的是在蓝色形状背景内重叠白色形状背景,以产生蓝色边框的效果。我无法想象这是获得这种效果的最佳方式。我看过其他试图解决这个问题的帖子,例如this和this,但我觉得它们与我的实现一样多。
有没有更好的方法或更标准的方法来简单地在某些视图(如 TextView)周围放置边框,还是我应该坚持我的做法?
编辑
我将 custom_bg_2.xml 更改为如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="2dp" android:color="#000000"/>
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
现在我得到了这个结果:
看起来我可以通过包含<stroke ... />
形状来实现轮廓。