java - 如何通过 Drawable 文件夹中的 XML 文件为 TextView 创建背景

标签 java android

以下是我想要实现的目标。

enter image description here

黑色粗方线是TextView尺寸的轮廓。圆角正方形周围的区域需要完全透明,以便背景可以从 TextView 所在的布局中显示出来。

我认为这是通过在 Drawable 文件夹中创建一个文件来实现的,该文件需要使用诸如 linessolid 等命令, 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/

相关文章:

java - @Nullable 和 SonarQube 'Conditionally executed blocks should be reachable' 警告

android - Android 上的 DTN(延迟容忍网络)

android - "ANR"是异常还是错误还是什么?

java - graphql-java - 如何在 spring boot 中使用订阅?

java - 使用枚举列表作为参数的 IN 关键字的 Spring Data 查询方法

javascript - 错误 : Jmeter: Typed variable declaration : Class or variable not found: org. openqa.selenium:在第 4 行

android - YouTubePlayerSupportFragment 空指针异常

带有自定义 URI 的 Android 深度链接

android - 在Android上捕获原始麦克风输入

java play 框架覆盖注释失败,java.source=1.6