首页 > 解决方案 > 我想在视图中添加笔触

问题描述

我正在尝试使用drawable将笔画添加到视图中。但我只想将它添加到侧面部分(左右),而不是整个部分。有什么好的方法可以在侧面部分添加笔画吗?

下面是我的可绘制 xml 代码。(leanear_border_gray.xml)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="2dp"
        android:color="#778899"/>

</shape>

下面是我的目标视图 xml 代码。

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/linear_border_gray"
        android:layout_weight="1">
       ....
   </LinearLayout>

标签: android

解决方案


您需要将您的矩形包裹起来shapelayer-list drawable添加描边并使用具有负值的顶部和底部边距“隐藏”顶部和底部线,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="-4dp" android:bottom="-4dp">
        <shape android:shape="rectangle">
           <stroke android:width="2dp" android:color="#778899"/>
        </shape>
    </item>
</layer-list>

推荐阅读