Create a Horizontal dotted line in android layout

后端 未结 2 1035
夕颜
夕颜 2020-12-28 11:32

in my layout I am trying to draw a DOTTED LINE.for drawing a horizontal line i am defining a view in my layout file.

     

        
相关标签:
2条回答
  • 2020-12-28 12:14

    I was pulling my hair on this issue too until I figured out there is a bug in the latest versions of Android when rendering lines like this.

    This bug can be circumvented by adding android:layerType="software" to the view which is using the dotted line as a background.

    Example:

    dotted.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    
    <stroke
        android:dashGap="3dp"
        android:dashWidth="8dp"
        android:height="2px"
        android:color="#FFFFFF" />
    
    </shape>
    

    layout.xml:

        <View
            android:id="@+id/vDottedLine"
            android:background="@drawable/dotted"
            android:layout_width="match_parent"
            android:layout_height="2px"
            android:layerType="software" />
    
    0 讨论(0)
  • 2020-12-28 12:30

    You can use following code . it may help you. Create a dotted.xml in drawable folder like this...

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="line" >
           <stroke
           android:color="#A52A2A"
           android:dashWidth="10px"
           android:dashGap="10px" />
    
    </shape>
    

    then use this xml in your layout with image view like this....

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is above line of code " />
        <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="3dp"
        android:src="@drawable/dottede" />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="this is below code " />
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题