所以我想要一个 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: Activity 的 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 ,但我觉得它们与我的实现一样多。
有没有更好的方法或更标准的方法来简单地在某些 View (例如 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 ... />
来实现大纲形状。
最佳答案
如果你添加一个
<?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" />
<stroke android:color="#439CC8" android:width="2dp" />
</shape>
关于android - 有没有更简单/更好的方法在我的 TextView 周围放置边框/轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5254562/