39

How do I add a horizontal 1px white line above image view in a relative layout?

<RelativeLayout
android:id="@+id/widget38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="108px"
android:layout_y="87px"
>  
<ImageView
android:id="@+id/widget39"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
>  
</ImageView>  
</RelativeLayout>
Jonik
  • 74,291
  • 66
  • 249
  • 356
dropsOfJupiter
  • 6,445
  • 11
  • 44
  • 59

2 Answers2

107

Just add the following line in your XML where ever you want it.

<View android:background="#ffffff" 
      android:layout_width = "match_parent" 
      android:layout_height="1dp"/>

Edit: Try this:

<RelativeLayout
android:id="@+id/widget38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="108px"
android:layout_y="87px"
>
<View android:id="@+id/separator" 
 android:background="#ffffff" 
 android:layout_width = "fill_parent"
 android:layout_height="1dip"
 android:layout_centerVertical ="true"
 android:layout_alignParentTop="true"/>
<ImageView
 android:id="@+id/widget39"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@id/separator"
 android:layout_alignParentRight="true"
/>  
</RelativeLayout>
RenatoIvancic
  • 1,036
  • 1
  • 14
  • 27
blindstuff
  • 17,740
  • 10
  • 45
  • 47
  • I added this and it works great, thank you! Except android:layout_above="@id/your_image_view_id" does not work, it constantly gives me an error. So I had to remove this attribute, but the line is hanging somewhere in the middle of the relative layout. – dropsOfJupiter Dec 13 '10 at 22:02
15

Consider moving the layout for the line into a separate file:

<!-- horizontal_line.xml -->
<?xml version="1.0" encoding="utf-8"?>
<View
    style="@style/HorizontalLine" />

... referencing a custom style definition:

<!-- styles.xml -->
<style name="HorizontalLine">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">@dimen/horizontal_line_height</item>
    <item name="android:background">@color/horizontal_line_fill_color</item>
    <item name="android:layout_marginTop">@dimen/large_spacer</item>
    <item name="android:layout_marginBottom">@dimen/large_spacer</item>
</style>

... and then you can include it in your layout:

<RelativeLayout
    android:id="@+id/widget38"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="108px"
    android:layout_y="87px" >

    <include
        android:id="@+id/horizontal_line"
        layout="@layout/horizontal_line" />

    <ImageView
        android:id="@+id/widget39"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/horizontal_line"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true" />

</RelativeLayout>
JJD
  • 44,755
  • 49
  • 183
  • 309
  • 9
    I like this approach since it reduces duplication and cleans up the layout files (+1). However, I'd skip the ``, and just use `` directly in the layout XMLs. Simpler and works the same. – Jonik Jul 02 '14 at 13:20