Search code examples
androidviewpaddingcenteralignment

Aligning center relative to others


So it's possible to align the top, bottom, left, and right of one view with another so that their edges are flush with each other. However, I've been wondering if it's possible to align the centers of two views of different sizes. My situation occurs when I have an ImageView side by side with a TextView, like this: [ImageView] [TextView]. The ImageView is bit taller than the TextView and so what I do is add padding/margins to the bottom of TextView to get it align and look like the ImageView and TextView horizontal centers are aligned. Problem is, when this view is displayed on larger tablet screens, the padding and margins don't work out right and the TextView doesn't look aligned with the ImageView. I'm sure there is an easy fix to allow this to always work out, so could someone provide me with some insights? Thanks!


Solution

  • This is how I did for a listview row:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#FFFFFF"
      android:padding="5dp"
      android:gravity="center_vertical">
      <TextView android:id="@+id/questionItemTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20sp"
        android:textColor="#000000"
        android:gravity="center_vertical"/>
      <ImageView android:id="@+id/questionViewed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/eye"
        android:layout_marginLeft="5dp" />
      <ImageView android:id="@+id/questionAnswered"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/check"
        android:layout_marginLeft="5dp" />
    </LinearLayout>