如何在 android 中设置视图的底部边框

how to set border bottom of a view in android

我正在尝试通过使用图层列表可绘制对象在 Android 中设置 TextView 的底部边框,但问题是我看到角落从底部稍微向上,但在 textview 下不是一条直线。我希望它是完美的直线而不是弯曲的。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <!-- draw a 4 dp width border around the rectangle shape -->
            <stroke android:color="@color/colorPrimary" android:width="2dp"/>
        </shape>
    </item>
    <!--
        hide left, top and right side border using white color
        by padding 4 dp bottom side
    -->
    <item android:bottom="3dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
        </shape>
    </item>
</layer-list>




  <TextView
        android:gravity="center"
        android:text="@string/eat"
        android:textSize="12sp"
        android:background="@drawable/border_bottom_green_tab"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

这对您有用,只需将 android:bottom

的 3dp 更改为 1dp
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">

            <stroke android:color="@color/colorPrimary" android:width="2dp"/>
        </shape>
    </item>

    <item android:bottom="1dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
        </shape>
    </item>
</layer-list>

只需在第一个 item

中使用 solid 而不是 stroke
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <!-- draw a 4 dp width border around the rectangle shape -->
        <solid android:color="@color/colorPrimary" />
    </shape>
</item>
<!--
    hide left, top and right side border using white color
    by padding 4 dp bottom side
-->
<item android:bottom="3dp">
    <shape android:shape="rectangle">
        <solid android:color="@android:color/white" />
    </shape>
</item>