3
votes

I have read other questions about adding a vertical line in Android, here's my questions

  1. I was able to add a vertical line as a "View" under my main RelativeLayout, but fails to do it within sub RelativeLayout.

code for a vertical line

<View
android:id="@+id/verticalSeperatorHours"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="@color/medium_dark_gray" 
/>

code for a horizontal line:

<LinearLayout
            android:id="@+id/currentTimeMarkerLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="100dp"
            android:baselineAligned="false"
            android:orientation="horizontal"
            android:padding="0dp" >

            <View
                android:layout_width="0dp"
                android:layout_height="3dp"
                android:layout_weight="1" />

            <View
                android:id="@+id/currentTimeLineView"
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_weight="14"
                android:background="@color/strong_blue" />
        </LinearLayout>

My structure xml:

  • RelativeLayout
    • LinearLayout
    • ScrollView
      • RelativeLayout
        • RelativeLayout
        • "Adding a vertical line doesn't work"
        • "Adding a horizontal line works"
  • LinearLayout
  • Adding a vertical line here works but not what I want"

The "View" after the LinearLayout of horizontal line didn't appear. I realized that I didn't understand the structure of android layout in deep, so can someone explain it to me how it works.

Complete Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:arm="http://schemas.android.com/apk/res/edu.cmu.sv.arm"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="0dp" >

<LinearLayout ...


<View
    android:id="@+id/dividerView"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/dayLabelsLinearLayout"
    android:background="@color/medium_gray" />

<ScrollView
    android:id="@+id/calendarScrollView"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/dividerView"
    android:layout_alignParentBottom="true"
    android:overScrollMode="never"
    android:padding="0dp"
    android:scrollbars="none"
    android:fadingEdge="none" >

    <RelativeLayout
        android:id="@+id/calendarRelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="0dp" >

        <LinearLayout ...


        <LinearLayout
            android:id="@+id/currentTimeMarkerLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="100dp"
            android:baselineAligned="false"
            android:orientation="horizontal"
            android:padding="0dp" >

            <View
                android:layout_width="0dp"
                android:layout_height="3dp"
                android:layout_weight="1" />

            <View
                android:id="@+id/currentTimeLineView"
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_weight="14"
                android:background="@color/strong_blue" />
        </LinearLayout>
        <View
                android:id="@+id/verticalSeperatorHours"
                android:layout_width="1dip"
                android:layout_height="match_parent"
                android:layout_marginLeft="100dp"
                android:background="@color/medium_dark_gray" />
    </RelativeLayout>
</ScrollView>



<LinearLayout ...

<View ...

</RelativeLayout>
3
Well that was a new way of showing your layout. Please edit and show your complete layout XML instead of just your "structure" and your "horizontal line".Simon Forsberg

3 Answers

7
votes

You should complete the LinearLayout after you use View.

I used the following code in my XML file, and it worked.

It should work for you as well.

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="1dp"
     android:paddingLeft="5dip"
     android:paddingRight="5dip">
<View 
    android:id="@+id/view"
    android:layout_toRightOf="@+id/text1"
    android:layout_width="1dip"
    android:layout_height="fill_parent"
    android:background="#FF0000FF"/>
 </LinearLayout>
0
votes

i used shape for set vertical and horizontal line.

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:background="@drawable/drawable_profile_week_back"
        android:gravity="right"
        android:paddingLeft="1dip"
        android:paddingRight="1dip"
        android:orientation="vertical"
        android:minHeight="60dp"
        android:layout_height="60dp"   >



    </LinearLayout>

drawable => drawable_profile_week_back

sion="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="-10dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="#a6a6a6" />
        </shape>
    </item>

    <item
        android:bottom="-10dp"
        android:left="60dp"
        android:right="-10dp"
        android:top="-10dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="#a6a6a6" />
        </shape>
    </item>

    <item
        android:bottom="-10dp"
        android:left="60dp"
        android:right="-10dp"
        android:top="14dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="#a6a6a6" />
        </shape>
    </item>

    <item
        android:bottom="-10dp"
        android:left="60dp"
        android:right="-10dp"
        android:top="29dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="#a6a6a6" />
        </shape>
    </item>

    <item
        android:bottom="-10dp"
        android:left="60dp"
        android:right="-10dp"
        android:top="44dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="#a6a6a6" />
        </shape>
    </item>

</layer-list>
0
votes

try this code for vertical line

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:layout_marginTop="100dp"
    android:background="#000000" />

</LinearLayout>

enter image description here

or

<TextView
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:layout_marginTop="100dp"
    android:background="#000000" />