代码之家  ›  专栏  ›  技术社区  ›  Jung Jaehoon

我想在视图中添加笔划

  •  1
  • Jung Jaehoon  · 技术社区  · 6 年前

    我正在尝试使用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>
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Demigod    6 年前

    你需要把你的 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>
    
        2
  •  1
  •   Parth Patel Praful Argiddi    6 年前

    你可以用 layer-list drawable 由@idemagod建议

    你的布局应该是这样的:

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_marginTop="20dp"
        android:background="@drawable/border_left_right"
        android:layout_gravity="center"
        android:layout_weight="1">
    
    </LinearLayout>
    

    音符: 如果你在付出 android:layout_height="match_parent" android:layout_weight="1" 那你就必须 android:layout_width="0dp" 反之亦然 android:layout_width="match_parent" .

    文件: border_left_right.xml左/右边框

    <?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="#07060b"/>
        </shape>
    </item>