android - 有没有更简单/更好的方法在我的 TextView 周围放置边框/轮廓?

标签 android xml textview border android-ui

所以我想要一个 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>

结果:

picture of blue border

我在这里所做的是将白色形状背景重叠在蓝色形状背景中,以产生蓝色边框的效果。我无法想象这是获得这种效果的最佳方式。我看过其他试图解决此问题的帖子,例如 thisthis ,但我觉得它们与我的实现一样多。

有没有更好的方法或更标准的方法来简单地在某些 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>

现在我得到了这个结果:

blue and black border

看起来我可以通过包含 <stroke ... /> 来实现大纲形状。

最佳答案

如果你添加一个 部分,你可能只需要 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" />
    <stroke android:color="#439CC8" android:width="2dp" />
</shape>

关于android - 有没有更简单/更好的方法在我的 TextView 周围放置边框/轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5254562/

相关文章:

java - Arraylist 将最后一个元素读取为倒数第二个元素

android - 在 Android 的后台线程中运行 GPS 定位

c# - 在大型 XML 文档中查找特定属性

android - 是否可以在不分配额外内存的情况下不断更新 TextView 文本?

java - Firebase 存储中的 'getBytes()' 和 'getStream()' 方法有什么区别?

android - Fragment 的 onCreateView() 导致我的应用程序崩溃

c# - 使用 Unity XML 配置文件在构造函数中注入(inject)复杂对象

java - 启动画面不会显示,只有空白区域

java - 使用按钮多次更新 TextView

android - 如果另一个 TextView 太长,如何在新行中移动 TextView