以下是我想要实现的目标。
黑色粗方线是TextView
尺寸的轮廓。圆角正方形周围的区域需要完全透明,以便背景可以从 TextView
所在的布局中显示出来。
我认为这是通过在 Drawable
文件夹中创建一个文件来实现的,该文件需要使用诸如 lines
、solid
等命令, corners
和类似的东西,但我不知道从那里去哪里。我很难找到任何完整而彻底的教程来帮助解决这个问题。下面是我开始的地方,但无法进一步深入。
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="@android:color/transparent"/>
<stroke
android:width="2dp"
android:color="#000000"/>
<corners
android:radius="8dp" />
</shape>
但这与我上面的图片并不接近。我的问题是如何调整上面的代码以获得我想要的图片?
最佳答案
尝试这个形状:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:endColor="#000000" android:startColor="#000000" />
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
</shape>
然后你像这样设置你的文本字段:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/roundedView"
/>
关于java - 如何通过 Drawable 文件夹中的 XML 文件为 TextView 创建背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20954343/