android - 我想在 View 中添加笔画

标签 android

我正在尝试使用 drawable 向 View 添加笔画。 但我只想在侧面部分(左侧和右侧)添加它,而不是整个部分。 有没有什么好的方法可以在侧面部分添加描边?

下面是我的可绘制 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>

下面是我的目标 View 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>

最佳答案

您需要将您的 shape 包装到具有矩形形状的 layer-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>

关于android - 我想在 View 中添加笔画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50717693/

相关文章:

android - 我可以将多少个广告横幅添加到 android 布局?

java - 如何更改 Sqlite 数据库中特定数据的 Listview 中的行颜色

java - 如何使用 Intent 在主要 Activity 布局之上打开 Activity ?

android - 使用电话号码格式 NaN 屏蔽 EditText,如 PhoneNumberUtils

android - 当测试项目在 libs 目录中有 jar 时,无法构建和运行使用 "ant create test-project"创建的 android 测试项目

android - 如何模拟一个服务被Android系统杀死

android - 错误 : R cannot be resolved

java - 无法解析符号 c882c94be45fff9d16a1cf845fc16ec5

android--如何使用measure getPressure

android - 如何使用相对布局垂直对齐列表中的项目?