How to put Divider in Relative Layout Android?

this is my code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="?android:attr/selectableItemBackground" >


<ImageView
    android:id="@+id/item_icon"
    android:layout_width="30dp"
    android:layout_height="20dp"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="12dp"
    android:layout_marginStart="12dp" />

<TextView
    android:id="@+id/item_name"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingRight="20dp"
    android:paddingEnd="10dp"
    android:text="Test"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />




</RelativeLayout>

      

I tried to put this partial code at the end of my Relativelayout but it doesnt show the separator

this is my divider code:

<View
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="#000000"
    android:layout_below="@id/item_name"
   />

      

so how can i show this separator below my text view?

+3


source to share


6 answers


    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="?android:attr/selectableItemBackground" >


    <ImageView
        android:id="@+id/item_icon"
        android:layout_width="30dp"
        android:layout_height="20dp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="12dp"
        android:src="@drawable/ic_launcher"/>

    <TextView
        android:id="@+id/item_name"
        android:textColor="@color/list_item_title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:paddingRight="20dp"
        android:paddingEnd="10dp"
        android:text="Test"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"
        android:layout_alignBottom="@id/item_name" />



</RelativeLayout>

      



+4


source


Replace the separator with



<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000000"
    android:layout_alignBottom="@id/item_name" />

      

0


source


replace the separator with

<View
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000000"
android:layout_alignParentBottom="true"/>

      

0


source


try it

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="?android:attr/selectableItemBackground" >

    <ImageView
        android:id="@+id/item_icon"
        android:layout_width="30dp"
        android:layout_height="20dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="12dp" />

    <TextView
        android:id="@+id/item_name"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:paddingEnd="10dp"
        android:paddingRight="20dp"
        android:text="Test"
        android:textAppearance="?android:attr/textAppearanceListItemSmall" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_alignBottom="@id/item_name"
        android:background="#000000" />

</RelativeLayout>

      

0


source


Try changing the height TextView

.

Change this:

android:layout_height="match_parent"

      

with this:

android:layout_height="wrap_content"
android:layout_weight="1"

      

I am assuming the text cover will hide your divider. If you do layout_weight="1"

, the text box fills the "free" space of your layout and match_parent

fills the parent view.

More on MATCH_PARENT here: http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT

0


source


try it

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground" >

    <ImageView
        android:id="@+id/item_icon"
        android:layout_width="30dp"
        android:layout_height="20dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="12dp" />

    <TextView
        android:id="@+id/item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:paddingEnd="10dp"
        android:paddingRight="20dp"
        android:text="Test"
     android:textAppearance="?android:attr/textAppearanceListItemSmall"     />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_below="@id/item_name"
        android:background="#000000"
        android:gravity="center" />
</RelativeLayout>

      

0


source







All Articles