Vertical line using XML drawable

前端 未结 15 600
无人及你
无人及你 2020-11-27 09:57

I\'m trying to figure out how to define a vertical line (1dp thick) to be used as a drawable.

To make a horizontal one, it\'s pretty straightforward:



        
相关标签:
15条回答
  • 2020-11-27 10:17
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle" >
        <stroke android:width="1dp" android:color="@color/white" />
        <size android:width="2dp" />
    </shape>
    

    Work's for me . Put it as background of view with fill_parent or fixed sized in dp height

    0 讨论(0)
  • 2020-11-27 10:17
     <View
            android:layout_width="2dp"
            android:layout_height="40dp"
    
            android:background="#ffffff"
            android:padding="10dp" />`
    
    0 讨论(0)
  • 2020-11-27 10:19
    add this in your styles.xml
    
            <style name="Divider">
                <item name="android:layout_width">1dip</item>
                <item name="android:layout_height">match_parent</item>
                <item name="android:background">@color/divider_color</item>
            </style>
    
            <style name="Divider_invisible">
                <item name="android:layout_width">1dip</item>
                <item name="android:layout_height">match_parent</item>
            </style>
    
    then wrap this style in a linear layout where you want the vertical line, I used the vertical line as a column divider in my table. 
    
         <TableLayout
                    android:id="@+id/table"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:stretchColumns="*" >
    
                    <TableRow
                        android:id="@+id/tableRow1"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:background="#92C94A" >
    
                        <TextView
                            android:id="@+id/textView11"
                            android:paddingBottom="10dp"
                            android:paddingLeft="5dp"
                            android:paddingRight="5dp"
                            android:paddingTop="10dp" />
        //...................................................................    
    
                        <LinearLayout
                            android:layout_width="1dp"
                            android:layout_height="match_parent" >
    
                            <View style="@style/Divider_invisible" />
                        </LinearLayout>
            //...................................................................
                        <TextView
                            android:id="@+id/textView12"
                            android:paddingBottom="10dp"
                            android:paddingLeft="5dp"
                            android:paddingRight="5dp"
                            android:paddingTop="10dp"
                            android:text="@string/main_wo_colon"
                            android:textColor="@color/white"
                            android:textSize="16sp" />
      //...............................................................  
                        <LinearLayout
                            android:layout_width="1dp"
                            android:layout_height="match_parent" >
    
                            <View style="@style/Divider" />
                        </LinearLayout>
    
        //...................................................................
                        <TextView
                            android:id="@+id/textView13"
                            android:paddingBottom="10dp"
                            android:paddingLeft="5dp"
                            android:paddingRight="5dp"
                            android:paddingTop="10dp"
                            android:text="@string/side_wo_colon"
                            android:textColor="@color/white"
                            android:textSize="16sp" />
    
                        <LinearLayout
                            android:layout_width="1dp"
                            android:layout_height="match_parent" >
    
                            <View style="@style/Divider" />
                        </LinearLayout>
    
                        <TextView
                            android:id="@+id/textView14"
                            android:paddingBottom="10dp"
                            android:paddingLeft="5dp"
                            android:paddingRight="5dp"
                            android:paddingTop="10dp"
                            android:text="@string/total"
                            android:textColor="@color/white"
                            android:textSize="16sp" />
                    </TableRow>
    
                    <!-- display this button in 3rd column via layout_column(zero based) -->
    
                    <TableRow
                        android:id="@+id/tableRow2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#6F9C33" >
    
                        <TextView
                            android:id="@+id/textView21"
                            android:padding="5dp"
                            android:text="@string/servings"
                            android:textColor="@color/white"
                            android:textSize="16sp" />
    
                        <LinearLayout
                            android:layout_width="1dp"
                            android:layout_height="match_parent" >
    
                            <View style="@style/Divider" />
                        </LinearLayout>
    
        ..........
        .......
        ......
    
    0 讨论(0)
  • 2020-11-27 10:20

    It looks like no one mentioned this option:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:drawable="@color/white" android:width="1dp"/>
    </layer-list>
    
    0 讨论(0)
  • 2020-11-27 10:21

    Depends, where you want to have the vertical line, but if you want a vertical border for example, you can have the parent view have a background a custom drawable. And you can then define the drawable like this:

    <?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="#000000" />
                <solid android:color="#00ffffff" />
    
            </shape>
        </item>
    
        <item android:right="1dp">
            <shape android:shape="rectangle">
                <solid android:color="#00ffffff" />
            </shape>
        </item>
    
    </layer-list>
    

    This example will create a 1dp thin black line on the right side of the view, that will have this drawable as an background.

    0 讨论(0)
  • 2020-11-27 10:23

    You can use the rotate attribute

     <item>
        <rotate
            android:fromDegrees="90"
            android:toDegrees="90"
            android:pivotX="50%"
            android:pivotY="50%" >
            <shape
                android:shape="line"
                android:top="1dip" >
                <stroke
                    android:width="1dip"
                     />
            </shape>
        </rotate>
    </item>
    
    0 讨论(0)
提交回复
热议问题