2012-08-06 119 views
2

我有一個TextView和3 Buttons居中在RelativeLayout,並且其中的所有三個文本左對齊父中心作爲最左邊的行。安卓文本將不會與中心對齊

我試圖改變的TextViewButtons和佈局的gravity,但沒有什麼工作,我很困惑,因爲我從來沒有見過的Android對齊文本是這樣的。

我希望Button文字以每個Button爲中心,頂端文字居中對齊。

的是什麼樣子的圖像是在這裏:http://i.stack.imgur.com/9lHEd.png

這裏是我的xml:

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

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_margin="20dp" 
    android:gravity="center_horizontal" 
    android:text="@string/greeting" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<Button 
    android:id="@+id/startQuizButton" 
    android:layout_width="280dp" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:onClick="@string/startQuiz" 
    android:text="@string/start_quiz" /> 

<Button 
    android:id="@+id/lessonsButton" 
    android:layout_width="280dp" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/startQuizButton" 
    android:layout_centerHorizontal="true" 
    android:onClick="@string/startLesson" 
    android:text="@string/lessonsString" /> 

<Button 
    android:id="@+id/viewAllButton" 
    android:layout_width="280dp" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/lessonsButton" 
    android:layout_centerHorizontal="true" 
    android:onClick="@string/showAll" 
    android:text="@string/view_all" /> 

回答

5

我包括一個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" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:text="Greetings" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Start Quiz" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Lessons" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="View all questions" /> 
    </LinearLayout> 

</RelativeLayout> 

enter image description here

+0

我剛剛嘗試過您的代碼,並且仍然按照原始屏幕截圖對齊文本。我會嘗試刪除相關的佈局,看看它做了什麼。 – Jordan 2012-08-06 14:52:54

+0

奇怪的是,我測試了一個實際的手機和我發佈的作品。我會發布我正在獲取的屏幕截圖。 – prolink007 2012-08-06 14:54:09

+0

已發佈屏幕截圖。 – prolink007 2012-08-06 14:56:15

1

添加按鈕標籤的android:比重= 「中心」 ......看看它的作品..

+0

這產生了s結果是我的原始代碼。 – Jordan 2012-08-06 14:55:14

0

如果要居中的一些文字,然後你也可以這樣來做:

<TextView 
    android:text="Quantity" 
    android:textSize="34sp" 
    android:textColor="#ffffff" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:fontFamily="sans-serif-light" 
    android:layout_centerHorizontal="true" 
    android:paddingTop="10dp"/> 

在這裏,我已經改變使它horozontal使用android:layout_centerHorizontal="true"這需要一個布爾值真值或假值,通過使其爲真,它將自動以所有設備爲中心 希望有幫助

+0

android:layout_centerHorizo​​ntal =「true」是正確的答案。謝謝@carl – 2015-12-16 08:04:51