1

I want to draw a line as ImageView's background.But it doesn't work?

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
    <solid android:color="#FF0000FF" />
    <size android:width="10dp"
        android:height="2dp"/>
</shape>

Just a demo.

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/dividing_line"
    android:layout_marginLeft="10dp" />
Egos Zhang
  • 1,334
  • 10
  • 18

1 Answers1

0

ImageView has no content so size will be 0x0dp. If you want a divider try setting the width to match_parent and the height to whatever you feel comfortable with (2dp for example).

Update: Try this.

In your layout file:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="4dp"
    android:background="@drawable/dividing_line"
    android:layout_marginLeft="10dp" />

The drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
    <stroke android:width="2dp" android:color="#0000FF"/>
    <size android:height="2dp" />
</shape>

It is working for me.


As pointed out by TheTool if you just want a divider the easier solution is to add

<View android:layout_width="match_parent" 
      android:layout_height="2dp" 
      android:background="@color/yourColor" />

to your layout file.

Community
  • 1
  • 1