2011-11-09 128 views
0

我要實現我的Android應用程序之一以下佈局:什麼是最有效的方式來實現這種佈局

Layout to build

編輯:

我還需要一個的選擇要顯示的元件以類似的方式爲:

enter image description here

所以各3周的Elemen的ts必須佔據水平空間的1/3才能使選擇足夠大。

/編輯

我無法找到最有效的方法來做到這一點。

我第一次嘗試:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/forums_navbar" 
    android:layout_width="fill_parent" android:layout_height="@dimen/forums_navbar_height" 
    android:background="@color/white"> 
    <TextView android:id="@+id/forums_navbar_TextView_debut" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="@string/forums_nav_debut" 
     android:textStyle="bold" 
     android:textColor="@color/forum_nav_blue" 
     android:textSize="15sp" 
     android:drawablePadding="4dip" 
     android:drawableLeft="@drawable/nav_fast_backward"> 
    </TextView> 
    <TextView android:id="@+id/forums_navbar_TextView_precedent" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="@string/forums_nav_precedent" 
     android:textStyle="bold" 
     android:textColor="@color/forum_nav_blue" 
     android:textSize="15sp" 
     android:drawablePadding="4dip" 
     android:drawableLeft="@drawable/nav_backward"> 
    </TextView> 
    <TextView android:id="@+id/forums_navbar_TextView_suivant" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="@string/forums_nav_suivant" 
     android:textStyle="bold" 
     android:textColor="@color/forum_nav_blue" 
     android:textSize="15sp" 
     android:drawablePadding="4dip" 
     android:drawableRight="@drawable/nav_fast_forward"> 
    </TextView> 
</LinearLayout> 

結果不正是預期:

Bad one

我已經改變了佈局,它的工作:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/forums_navbar" 
    android:layout_width="fill_parent" android:layout_height="@dimen/forums_topics_navbar_height" 
    android:background="@color/white"> 
    <RelativeLayout 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:layout_weight="1"> 
     <TextView android:id="@+id/forums_navbar_TextView_debut" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:text="@string/forums_nav_debut" 
      android:textStyle="bold" 
      android:textColor="@color/forum_nav_blue" 
      android:textSize="15sp" 
      android:drawablePadding="4dip" 
      android:drawableLeft="@drawable/nav_fast_backward"> 
     </TextView> 
    </RelativeLayout> 
    <RelativeLayout 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:layout_weight="1"> 
     <TextView android:id="@+id/forums_navbar_TextView_precedent" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:text="@string/forums_nav_precedent" 
      android:textStyle="bold" 
      android:textColor="@color/forum_nav_blue" 
      android:textSize="15sp" 
      android:drawablePadding="4dip" 
      android:drawableLeft="@drawable/nav_backward"> 
     </TextView> 
    </RelativeLayout> 
    <RelativeLayout 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:layout_weight="1"> 
     <TextView android:id="@+id/forums_navbar_TextView_suivant" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:text="@string/forums_nav_suivant" 
      android:textStyle="bold" 
      android:textColor="@color/forum_nav_blue" 
      android:textSize="15sp" 
      android:drawablePadding="4dip" 
      android:drawableRight="@drawable/nav_fast_forward"> 
     </TextView> 
    </RelativeLayout> 
</LinearLayout> 

結果:

good

它的工作原理,但它根本沒有效率。有沒有更好的辦法?

+0

只是改變textviews的重力在你的第一個佈局:(按順序) '安卓重力= 「左」','安卓重力= 「中心」','安卓重力=」 right「' – Selvin

+0

實際上,TextView中文本的位置在正確的位置。 drawable不是。 –

+0

爲什麼?這是我得到你的第一個佈局和「重力修復」http://esilo.pl/selvin/layout1.png(android:gravity =「left」,android:gravity =「left」,android:gravity =「right 「) – Selvin

回答

4

你只需要有RelativeLayout在頂部。對於左側視圖

  1. 使用android:layout_alignParentLeft="true"
  2. 使用android:layout_centerHorizontal="true"爲中心的視圖
  3. 使用android:layout_alignParentRight="true"爲右視圖

輸出:

enter image description here

你可以有上面的輸出通過使用下面解決方案:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Left" 
     android:layout_alignParentLeft="true"> // For view at Left side 
    </TextView> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Center" 
     android:layout_centerHorizontal="true"> // For view at center 
    </TextView> 


    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Right" 
     android:layout_alignParentRight="true"> // for view at Right side 
    </TextView> 

</RelativeLayout> 
+0

這不正是我想要的。我需要讓每個元素位於屏幕的1/3處,並且「左」和「右」TextView需要以1/3的空間爲中心。在你的解決方案中,堅持屏幕邊框。 –

+1

@ol_v_er我親愛的你應該正確地提到你的要求。檢查所有的答案都來了相同的解決方案:( –

+1

抱歉,你是對的,設計師堅持「Début」和「Suivant」的屏幕邊界。我的第一個評論是不正確的。我要去喝咖啡... –

1

爲什麼不使用relativelayout作爲頂層,然後將三個組放在每個linearlayout中。 然後對於每個組(linearlayout),您可以將android:layout_alignParent設置爲居中,左側和右側。

+0

親愛的你是對的,但爲什麼需要將LinearLayout作爲三組的子佈局? –

+0

我雖然他在其中有超過一個TextView更:)但他不,所以我想不需要linearlayouts :) – Warpzit

0

我看到兩個選項:

  1. 作爲Warpzit說,使用RelativeLayout。這應該很容易做到這一點:只需對齊中間的一個(android:layout_centerHorizontal="true")和其他的左邊和右邊(android:layout_alignParentLeft="true",右邊相同)。
  2. 隨着LinearLayout,你可以有三個TextViewandroid:layout_width="0px"(是的,零像素)和layout_weight="1"。這將使LinearLayout給予TextView等寬。現在,您只需將文本分別對齊TextView中的每一箇中的左側,中間和右側,您可以使用android:gravity執行此操作。
+0

只要檢查實際的例子爲1點。 –

0
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:background="#FFFFFF" 
    android:weightSum="1"> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="20dp" 
     android:background="#FF0000" 
     android:layout_weight="0.33"> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="match_parent" 
     android:layout_height="20dp" 
     android:background="#00FF00" 
     android:layout_weight="0.34"> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout3" 
     android:layout_width="match_parent" 
     android:layout_height="20dp" 
     android:background="#0000FF" 
     android:layout_weight="0.33"> 

    </LinearLayout> 
</LinearLayout> 
相關問題