Android: How can i use the layer-list and shape elements to draw a horizontal rule when set as background?

扶醉桌前 提交于 2020-01-19 07:14:49

问题


I want to set the background of a Relative or LinearLayout to a custom drawable. I want the shape to draw two horizontal lines across the bottom, leaving the central part transparent (empty).

The following draws the horizontal lines centered vertically, where as i need them to be aligned to the bottom of the shape. (If you add a rectangle as an item you can see the shape expands to the dimenstions of the parent, but the lines are still centrally aligned).

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
 <item>
  <shape android:shape="line">
      <stroke android:width="1dip" android:color="#99b8b9bd" />
      <size android:height="1dip" />
  </shape>
 </item>
 <item>
  <shape android:shape="line" android:top="1dip">
      <stroke android:width="1dip" android:color="#FFFFFFFF" />
      <size android:height="1dip" />
  </shape>
 </item>
</layer-list>

回答1:


Found the answer before i'd finished asking about it.

<?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:width="1dp" android:color="@color/hr_bottom" />
            <solid android:color="#00FF0000" />
            <padding android:bottom="1dp"/>
        </shape>
   </item>

   <item>
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="@color/hr_top" />
            <solid android:color="#00FF0000" />
            <padding android:bottom="1dp"/>
        </shape>
   </item>

    <item>
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#00000000" />
            <solid android:color="#00000000" />
        </shape>
   </item>

</layer-list>



回答2:


You can draw with below simple code also.

<item
    android:right="-2dp"
    android:bottom="-2dp"
    >
    <shape
        android:shape="rectangle">

        <stroke
            android:width="0.5dp"
            android:color="#ff0000"
            android:dashGap="4dp"
            android:dashWidth="4dp" />

    </shape>
</item>



来源:https://stackoverflow.com/questions/4146795/android-how-can-i-use-the-layer-list-and-shape-elements-to-draw-a-horizontal-ru

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!