如何在不通过文本的情况下让一行环绕 textview?

How do I get a line to wrap around textview without passing through the text?

我打算实现这样的目标:

---------------Text---------------

其中“-----”实际上是一条线,(也许是视图线?或分隔线)。

更准确地说,这是我正在努力实现的一个例子: 我想知道如何在我的文本视图中实现这一点。这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/mydate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@android:color/darker_gray"
        android:textSize="18sp"/>
</RelativeLayout>

您可以通过创建一条全宽线,然后将 TextView 放在它上面来实现。如果您为 TextView 提供与父级背景相同的背景颜色,并为 TextView 提供一些左右填充,它看起来就像您的示例。

您可以将此用于行(line.xml 在您的 res/drawable 目录中):

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line"
>
    <stroke
        android:color="@android:color/darker_gray"
        android:width="1dp"
    />
</shape>

然后使用这个布局:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:src="@drawable/line"
        android:layout_gravity="center"
    />
    <TextView
        android:id="@+id/mydate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:background="@android:color/white"
        android:textColor="@android:color/darker_gray"
    />
</FrameLayout>