如何在包含按钮的Android标题栏中居中文本?

时间:2012-12-20 11:11:06

标签: android layout

我有一个ListView,它在View顶部包含一个LinearLayout,用作标题栏。列表可以滚动,但标题栏保持不变。在标题栏中,我想要一个“后退”按钮,但这会导致标题文本无法居中:

uncentered text 我添加了一个相同大小的第二个按钮(在标题栏的右侧)并使其不可见,以便作为文本中心的“间隔”,但后来我松散了很多标题栏实际 - 房地产:

enter image description here

有没有办法告诉Android将标题文本居中并忽略后退按钮?也许这只是生活的方式,但我认为在继续使用第二个隐形按钮之前我会运行它。 感谢

7 个答案:

答案 0 :(得分:2)

您可以使用RelativeLayout作为标题,而不是LinearLayout。并使用布局参数,以便:

  • 后退按钮与其父级的左边缘对齐。
  • TextView将整个父级(LayoutParam.FILLPARENT)与重力CENTER
  • 一起使用

答案 1 :(得分:2)

Yo可以使用相对布局而不是linearlayout,

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffaa" >

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="false"
            android:gravity="center"
            android:text="Button" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="false"
            android:gravity="center"
            android:paddingLeft="100dp"
            android:paddingRight="100dp"
            android:text="Header Title Embiggeden"
            android:textSize="20dp" />
    </RelativeLayout>

答案 2 :(得分:2)

这是给你的。我在我的项目中这个。

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#FF00FF"
            android:orientation="horizontal" >

        <Button
            android:id="@+id/testbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="Back" >
        </Button>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Header"
            android:gravity="center_horizontal"
            android:textSize="30sp" ></TextView>
       </RelativeLayout>

        <ListView
            android:id="@id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="40dp"
            android:divider="@android:color/transparent"
            android:dividerHeight="10sp" >
        </ListView>



    </RelativeLayout>

这是您需要的,并根据您的需要更改第二个RelativeLayout的颜色。

答案 3 :(得分:1)

然后尝试使用相对布局, 文字在

android:layout_centerHorizontal="true"

中的两个按钮
1.android:layout_toLeftOf="text id" 
2.android:layout_toRightOf="text id"

答案 4 :(得分:1)

relativelayout代替linearlayout。设置文字视图属性centerinvertical=true 并设置按钮alignParent=left.

答案 5 :(得分:1)

设置TextView的左侧填充等于不可见的按钮宽度

答案 6 :(得分:1)

您可以使用相对布局而不是LinearLayout

<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:background="#00ff00">

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:gravity="center" android:maxLines="1" 
        android:ellipsize="end" android:layout_centerInParent="true"/>

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" android:layout_centerVertical="true"/>

</RelativeLayout>

或者您可以使用FrameLayout

<FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:background="#00ff00">

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" />

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>

    </FrameLayout>