2011-04-11 85 views
13

我很新的android和試圖做一個示例應用程序,其中3個元素(2個按鈕和1個文本視圖)水平放置。我需要將文本置於中央,並在視圖的左側和右側對齊兩個按鈕。Android左,中間和右對齊

E.g.

|<Button> <Text>  <Button>| 

感謝您能幫助我。

在此先感謝

+0

如果您向我們顯示迄今爲止的代碼,它將更容易幫助您。 – Phonon 2011-04-11 16:19:10

回答

20

編輯:我相信這會解決您的問題。請參閱下面的代碼。讓我知道如果這有幫助!我知道這對我有幫助,我將會使用它。接受這個作爲答案,如果它的工作! =)

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:stretchColumns="1"> 
    <TableRow> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="button1" 
      android:id="@+id/button1button"></Button> 
     <EditText 
      android:layout_height="wrap_content" 
      android:id="@+id/firstedittext" 
      android:layout_width="wrap_content"></EditText> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="button2" 
      android:id="@+id/button2button"></Button> 
    </TableRow> 
</TableLayout> 

編輯:添加相對佈局,看看它是否適合你,讓我知道,如果它的工作原理。您需要調整「10px」以獲得所需的距離。我看到是否有更好的方式來做到這一點,距離更具活力。

您可以使按鈕「alignParentLeft」和「alignParentRight」使它們與屏幕的右側和左側對齊。但是,我仍然無法在兩個視圖之間獲取文本。

你可以在你的xml文件中進行佈局。將其設爲水平佈局,然後先添加第一個按鈕,然後添加文本視圖,然後添加第二個按鈕。稍後將舉一個例子來發布xml。

<RelativeLayout 
     android:id="@+id/LinearLayout01" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="button1" 
      android:layout_alignParentLeft="true" 
      android:layout_marginRight="10px" 
      android:id="@+id/button1button"></Button> 
     <EditText 
      android:layout_height="wrap_content" 
      android:id="@+id/firstedittext" 
      android:layout_toRightOf="@id/button1button" 
      android:layout_marginRight="10px" 
      android:layout_width="wrap_content"></EditText> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/firstedittext" 
      android:text="button2" 
      android:id="@+id/button2button"></Button> 
    </RelativeLayout> 
+0

感謝您的回覆。我試過這種方式。但他們沒有得到適當的對齊。 (這兩個按鈕需要對齊到屏幕的左側和右側。)我已將寬度設置爲填充父級。 – budsiya 2011-04-11 15:50:26

+0

我現在看到了,我修改了你的帖子,所以圖片會顯得更好。它正在等待審查。當它得到審查時,你應該看到它現在出現在你的文章中。就像你所說的那樣,我從來沒有真正對準視圖的元素。然而,當我有機會的時候,我會研究這個,看看我能否得到它的工作,因爲我也在尋找一個簡單的答案來解決這個問題。 – prolink007 2011-04-11 16:20:48

+0

非常感謝您的支持。真的很感激。一旦我今晚回家,我會嘗試這兩種方法。會讓你知道結果。 TX! – budsiya 2011-04-11 17:30:01

19

這樣做會有效果。你需要的是絕對重力。這個應該也適用於所有Android屏幕尺寸。我認爲您不需要使用任何邊距或填充物,一旦您的孩子擁有了內容,他們就會把你弄得一團糟(邊距在選擇它們方面是相對的,也就是說:當文本較少時,您希望邊距更小。 ..經常在翻譯,新功能,替換等過程中發生。)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="button1" 
     android:layout_alignParentLeft="true"/> 
    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/firstedittext" 
     android:layout_centerInParent="true"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="button2" 
     android:layout_alignParentRight="true"/> 
</RelativeLayout> 
+0

謝謝愛迪生......我今晚會試試看,並讓你知道結果。感謝你的幫助。 :) – budsiya 2011-04-11 17:27:57

+0

我在尋找無處不在的「layout_centerInParent」,無法找到它。我假設它會遵循與其他「layout_alignParentLeft」和「layout_alignParentRight」相同的命名。所以,我正在尋找「layout_alignParentCenter」,哈哈。 – prolink007 2011-04-11 17:35:17

+0

@ prolink007你使用的是最新的SDK嗎?我不確定什麼SDK給出了layout_x,但是如果你只是鍵入android:然後等待然後輸入「l」,那麼Eclipse應該爲你篩選出來......我通常只輸入「l」而不是試圖記住整個事情,因爲...我的記憶吸T_T。 – Edison 2011-04-12 05:27:52

0

這是我在LinearLayout上的代碼。希望它能幫助你!

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

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

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="start" 
      android:text="Button1" /> 
    </LinearLayout> 

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

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:ems="10" 
      android:text="Chao cac ban" > 

      <requestFocus /> 
     </EditText> 
    </LinearLayout> 

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

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="end" 
      android:text="Button2" /> 
    </LinearLayout> 

</LinearLayout>