操作栏中的自定义布局

时间:2013-11-21 20:34:35

标签: android android-layout android-actionbar

我正在尝试创建一个自定义布局作为操作栏。布局需要包含最右侧的图像,中间的TextView和最左侧的ImageView。我似乎无法集中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" />

1 个答案:

答案 0 :(得分:1)

使用RelativeLayout,它好多了,我认为这应该有用

<?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>