0

我正在尝试使用 ActionBarSherlock 实现自定义标题栏。标题栏的左右边缘应该有两个按钮,中间应该有一个文本视图。textview 应该占据两个按钮之间的空间。

我试过设置layout_weight,但由于某种原因它根本没有效果。

这是我在 res/layout 中的自定义布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<Button
    android:id="@+id/btn_left"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:text="Left" />

<TextView
    android:id="@+id/header"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:layout_weight="4"
    android:text="Header Text" />

<Button
    android:id="@+id/btn_right"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:text="Right" />

</LinearLayout>

现在所有元素都向左浮动,我不知道为什么,我做错了什么?

4

1 回答 1

0

尝试使用RelativeLayout 而不是使用LinerLayout。

编辑:使中心文本视图延伸到按钮(确保离开 TextView match_parent..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

    <Button android:id="@+id/leftButton" 
            android:layout_alignParentLeft="true"....

    <TextView android:layout_centerHorizontal="true"
              android:layout_toLeftOf="@id/rightButton"
              android:layout_toRightOf="@id/leftButton"  ....

    <Button android:id="@+id/rightButton"
            android:layout_alignParentRight="true"....


</RelativeLayout>
于 2013-05-18T23:54:57.540 回答