简体   繁体   中英

Center view between other two views in RelativeLayout

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?

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

This works for me:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llFullContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="top"
        android:id="@+id/tvTop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAlignment="center"
        android:layout_alignParentTop="true"/>

    <TextView
        android:text="middle"
        android:id="@+id/tvMiddle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tvBottom"
        android:layout_below="@+id/tvTop"
        android:gravity="center"
        android:textAlignment="center"/>

    <TextView
        android:text="bottom"
        android:id="@+id/tvBottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAlignment="center"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM