[英]How to center text in Android title bar containing a button?
我有一個ListView,它包含View頂部的LinearLayout,它作為標題欄。 列表可以滾動,但標題欄保持不變。 在標題欄中我想要一個“后退”按鈕,但這會導致標題文本變為非居中:
我添加了一個相同大小的第二個按鈕(在標題欄的右側)並使其不可見,以便作為文本中心的“間隔”,但后來我松散了很多標題欄實際 - 房地產:
有沒有辦法告訴Android將標題文本居中並忽略后退按鈕? 也許這只是生活的方式,但我認為在繼續使用第二個隱形按鈕之前我會運行它。 謝謝
您可以使用RelativeLayout作為標題,而不是LinearLayout。 並使用布局參數,以便:
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>
這個給你。 我已經在我的項目中解決了這個問題。
<?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的顏色。
然后嘗試使用相對布局,文本在
android:layout_centerHorizontal="true"
和兩個按鈕
1.android:layout_toLeftOf="text id"
2.android:layout_toRightOf="text id"
取relativelayout
而不是linearlayout
.set文本視圖屬性centerinvertical=true
並設置按鈕alignParent=left.
設置TextView左邊的填充等於不可見的按鈕寬度
您可以使用相對布局而不是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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.