2012-12-20 22 views
2

我有一个ListView,其中包含一个LinearLayout在视图的顶部,用作标题栏。列表可以滚动,但标题栏保持不变。在标题栏中,我想要一个“后退”按钮,但导致标题文本变得不居中:如何在包含按钮的Android标题栏中居中文字?

uncentered text 我添加了第二个具有相同大小的按钮(位于标题栏的右侧)和使得它无形的,以作为“间隔”文本居中上,但后来我失去了很多标题栏地产的:

enter image description here

有没有办法告诉安卓居中标题文本并忽略后退按钮?也许这只是生活的方式,但是我认为每个人都会先运行它,然后再继续看第二个隐藏的按钮或其他东西。 感谢

+0

是第3个按钮是不是显示? –

+0

您是否尝试了'FrameLayout' ..后框与文本视图和前框与后退按钮.. –

+0

nirav - 是的,我忘了让最右边的按钮在屏幕截图中可见。我更新了第一张截图以澄清。 – wufoo

回答

2

哟可以使用相对布局代替的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> 
+0

很好,谢谢! – wufoo

1

然后尝试使用相对布局, 的文本是

android:layout_centerHorizontal="true" 

1.android:layout_toLeftOf="text id" 
2.android:layout_toRightOf="text id" 
2

取而代之的是LinearLayout中的两个按钮,您可以使用RelativeLayout的为你的头。并使用布局参数如下:

  • 后退按钮与其父项的左侧边缘对齐。
  • TextView的取整父(LayoutParam.FILLPARENT)与重心
1

采取relativelayout代替linearlayout .SET文本视图属性centerinvertical=true 并设置按钮alignParent=left.

1

设置TextView左边的TextView等于不可见按钮宽度

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> 
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的颜色。

+0

我希望这是您的问题的完美解决方案。我已经测试过它。 – Unknown

相关问题