Android可绘制水平线

时间:2018-08-20 06:48:48

标签: android xml-drawable

是否可以使用xml来使行像这样?或有某种方法可以做到这一点..

enter image description here

我已经制作了一些可绘制的形状,但并不是真的想要

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:bottomLeftRadius="30dp"
        android:bottomRightRadius="30dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp" />
    <padding
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp" />
    <stroke
        android:width="0.8dp"
        android:color="@color/colorPrimaryDark" />
    <solid android:color="@android:color/transparent" />
</shape>

3 个答案:

答案 0 :(得分:0)

尝试一下

<?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="#de0b0b" />
            <solid android:color="#ffffff" />
            <corners android:radius="20dp" />
        </shape>
    </item>

    <item
        android:bottom="5dp"
        android:top="0dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="10dp"
                android:color="#ffffff" />
            <solid android:color="@android:color/transparent" />
            <corners android:radius="20dp" />
        </shape>
    </item>

</layer-list>

像这样使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:gravity="center"
    android:background="#ffffff"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/test"
        android:text="Nilesh"
        android:gravity="center"
        android:layout_margin="@dimen/_5sdp"
        android:padding="@dimen/_5sdp">
    </TextView>

</LinearLayout>

输出

enter image description here

编辑

尝试一下

<?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="#a032e4" />
            <solid android:color="#a032e4" />
            <corners android:radius="40dp"/>
        </shape>
    </item>

    <item android:bottom="4dp" >
        <shape
            android:shape="rectangle">
            <stroke android:width="2dp" android:color="#ffffff" />
            <solid android:color="#ffffff" />
            <corners android:radius="40dp"/>
        </shape>
    </item>

</layer-list>
  

喜欢这样

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:gravity="center"
    android:background="#FFFFFF"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/test"
        android:layout_margin="20dp"
        android:padding="10dp"
        android:text="Nilesh Rathod"
        android:gravity="center" />

</LinearLayout>

输出

enter image description here

答案 1 :(得分:0)

       <?xml version="1.0" encoding="utf-8"?>
        <inset xmlns:android="http://schemas.android.com/apk/res/android"
            android:insetLeft="-5dp"
            android:insetRight="-5dp"
            android:insetTop="-10dp">

       <shape android:shape="rectangle">
         <solid android:color="@android:color/transparent" />
           <stroke
             android:width="3dp"
             android:color="@color/colorPrimaryDark" />
       <corners
          android:bottomLeftRadius="25dp"
          android:bottomRightRadius="25dp" />
       </shape>
     </inset>

答案 2 :(得分:0)

我们也可以为此创建一个可绘制文件。可能对某人有帮助。

可绘制文件:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item>
    <shape android:shape="rectangle">
        <solid android:color="@android:color/white" />
    </shape>
</item>

<item>
    <shape android:shape="rectangle">
        <stroke
            android:width="5dp"
            android:color="@android:color/holo_blue_dark" />
        <solid android:color="@android:color/white" />
        <corners android:radius="20dp" />
    </shape>
</item>

<item android:bottom="25dp">
    <shape android:shape="rectangle">
        <stroke
            android:width="2dp"
            android:color="@android:color/white" />
        <solid android:color="@android:color/white" />
    </shape>
</item>

</layer-list>

结果:

enter image description here