简体   繁体   中英

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" />

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>

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