Sivam Kanagasabapathy
Sivam Kanagasabapathy

Reputation: 23

Custom layout in action bar

I'm trying to create a custom layout to put as the action bar. The layout needs to contain an image on the far right, TextView in the middle, and an ImageView on the far left. I can't seem to center the TextView.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#aaaa0000"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_menu_back"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="298dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="TextView" />

Upvotes: 0

Views: 313

Answers (1)

dumazy
dumazy

Reputation: 14435

Use RelativeLayout, it's a lot better, I think this should work

<?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="match_parent"
      android:background="#aaaa0000" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_menu_back"
    android:layout_alignParentLeft="true"

    />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="TextView" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/something_else"
    android:layout_alignParentRight="true"

    />
</RelativeLayout>

Upvotes: 1

Related Questions