I try to center one textview between two others in relative layout. One text view is aligned to parent top, one to parrent bottom and third one (@id/info) is placed inbetween. I have following view hierarchy of list item used in ListView:
<?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:listSelector="@drawable/list_selector"
android:orientation="vertical"
android:paddingBottom="@dimen/padding_default"
android:paddingLeft="@dimen/padding_large"
android:paddingRight="@dimen/padding_large"
android:paddingTop="@dimen/padding_default" >
<ImageView
android:id="@+id/image"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/logo_imageonly" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/image"
android:ellipsize="end"
android:paddingLeft="@dimen/padding_default"
android:singleLine="true"
android:textColor="@color/best_prices_item_text_default"
android:textSize="@dimen/text_size_medium"
android:textStyle="bold" />
<TextView
android:id="@+id/priceQty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/image"
android:ellipsize="end"
android:paddingLeft="@dimen/padding_default"
android:singleLine="true"
android:textColor="@color/best_prices_item_price"
android:textSize="@dimen/text_size_small" />
<TextView
android:id="@+id/gorceryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/priceQty"
android:gravity="right"
android:singleLine="true"
android:textColor="@color/best_prices_item_grocery_name"
android:textSize="@dimen/text_size_xsmall"
android:textStyle="italic" />
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/priceQty"
android:layout_below="@id/name"
android:layout_toRightOf="@id/image"
android:ellipsize="end"
android:gravity="center_vertical"
android:paddingLeft="@dimen/padding_default"
android:singleLine="true"
android:textColor="@color/best_prices_item_text_default"
android:textSize="@dimen/text_size_small" />
</RelativeLayout>
In Eclipse view designer effect is as I would expect - textview is properly centered:
But when I run app on device/emulator centered textview is missing. Has anybody bumped on this issue and can help?
When using the RelativeLayout , you can use android:layout_centerInParent="true" , which will do what it says, center it inside its parent.
To center a view, just drag the handles to all four sides of the parent.
We can use LinearLayout inside RelativeLayout. We can also use RelativeLayout as a Child of LinearLayout.
try to use android:layout_centerInParent="true"
, android:layout_centerVertical="true"
or android:layout_centerHorizontal="true"
. Hope that helps.. Sometimes its better to use couple of LinearLayouts with different orientations, inside other LinearLayouts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With