user2582318
user2582318

Reputation: 1647

Align 2 TextViews one under other but centered to their relative parent

I'm trying to center a TextView under another one (it's centering), but I'm getting this

enter image description here

The 2 items are not aligned correctly (as I'd want)

I'm looking for something like this:

enter image description here

You can see both items centered, not just the bigger one

my layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/r1"
android:clickable="true"
android:background="?android:selectableItemBackground"
android:layout_height="?android:attr/listPreferredItemHeight"
>
   <ImageButton
        android:id="@+id/status"
        android:layout_width="wrap_content"
    android:background="@null"
        android:paddingRight="6dp"
        android:layout_height="fill_parent"
        android:src="@drawable/download"
        android:layout_centerVertical="true" />
    <TextView
        android:id="@+id/descricao"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:text="Texto de baixo"
        android:singleLine="true"
        android:typeface="serif"
        android:textSize="10sp"
        android:layout_centerVertical="true"
        android:layout_below="@+id/titulo"
        android:layout_toRightOf="@+id/status" />
    <TextView
        android:id="@+id/titulo"
        android:textStyle="bold"
        android:text="Text Title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textSize="16sp"
        android:layout_toRightOf="@+id/status" />
</RelativeLayout>

EDIT

Thanks Der Golem, your answer made the trick, but I got another problem, the text when it gets more than one line, is there a way not to hide it behind the below text, and align it again?

enter image description here

Upvotes: 0

Views: 548

Answers (2)

Vijet Badigannavar
Vijet Badigannavar

Reputation: 552

Just change the layout_height of the parent layout to wrap_content from "?android:attr/listPreferredItemHeight"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/r1"
android:clickable="true"
android:background="?android:selectableItemBackground"
android:layout_height="wrap_content"
>
<ImageButton

        android:id="@+id/status"
        android:layout_width="wrap_content"
    android:background="@null"
        android:paddingRight="6dp"
        android:layout_height="fill_parent"
        android:src="@drawable/download"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/descricao"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:text="Texto de baixo"
        android:singleLine="true"
        android:typeface="serif"
        android:textSize="10sp"
        android:layout_centerVertical="true"
        android:layout_below="@+id/titulo"
        android:layout_toRightOf="@+id/status" />

    <TextView
        android:id="@+id/titulo"
        android:textStyle="bold"
        android:text="Text Title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textSize="16sp"
        android:layout_toRightOf="@+id/status" />

</RelativeLayout>

Upvotes: 1

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

You are nearly there: Observe the drawing.

Be the red line a generic View (let's give it an id of vwDummy), aligned to the center of the row (android:layout_centerInParent="true").

Let its dimensions be 0dp and 0dp.

Then Just put the title above it (android:layout_above="@id/vwDummy").
And put the lower text below it (android:layout_below="@id/vwDummy").

Upvotes: 1

Related Questions