2012-02-28 79 views
0

©對線性佈局有疑問。我覺得有些奇怪。所有這些都是相同的佈局,但後面的按鈕移動相關的圖標buton.I想適合左側的按鈕和btn_header_what_search_user按鈕想要正確的site.But他們正在移動相關的圖標(中心圖標)。爲什麼?以及如何退回左側的按鈕。Linearlayout重力問題?

enter image description here

我的代碼:`

<LinearLayout android:layout_height="44dip" 
       android:layout_width="320dip" 
       android:background="@drawable/header_320_44dip"> 
    <ImageView 
      android:layout_width="121dip" 
      android:layout_height="32dip" 
      android:layout_gravity="center" 
      android:layout_marginLeft="90dip" 
      android:background="@drawable/icon"> 

    </ImageView> 

    <ImageView 
      android:layout_width="52dip" 
      android:layout_height="32dip" 
      android:layout_gravity="left" 
      android:background="@drawable/btn_back_52x32"> 

    </ImageView> 


    <ImageButton 
      android:layout_height="34dip" 
      android:layout_width="64dip" 
      android:id="@+id/btn_header_what_search_user" 
      android:layout_marginLeft="0dip" 
      android:background="@drawable/btn_nedit_nontext_64x34"> 
    </ImageButton> 
</LinearLayout> 

`

回答

2

LinearLayout不能很好地與重力沿着它的主要方向(即水平的LinearLayout不起作用支持left,right或center_horizo​​ntal,以及垂直的top,bottom或center_vertical)。
您可以嘗試使用RelativeLayout並使用layout_alignParentLeft/layout_alignParentRight屬性(或任何其他可用的屬性)來獲取正確的結果。

+0

所以如果我使用線性佈局任何時候我不能使用對齊?如果我可以使用對齊我怎麼可以在線性佈局?而我最後的問題是爲什麼在添加項目之前關聯?這是正常的嗎?我在想移動相關的0,0點? – DuyguK 2012-02-28 13:10:13

+0

LinearLayout中的項目是從左到右和從上到下依次添加的。這就是爲什麼你的佈局中的項目安排受到干擾。 – Maneesh 2012-02-28 13:13:52

+0

不同的'align'屬性對'RelativeLayout'是唯一的,這意味着你不能在'LinearLayout'中使用它們。 'RelativeLayout'還具有「toLeftOf」,「toRightOf」,「above」和「below」的屬性,可用於相對於彼此定位視圖。在LinearLayout中,添加的每個視圖都按它們在xml中出現的順序出現,也就是說,您的圖標出現在後退按鈕之前,因此放置在它之前。 – Jave 2012-02-28 13:19:07

3

你應該使用相對佈局

<RelativeLayout android:layout_height="44dip" 
       android:layout_width="320dip" 
       android:background="@drawable/header_320_44dip"> 
    <ImageView 
      android:layout_width="121dip" 
      android:layout_height="32dip" 
      android:layout_centerInParent="true" 
      android:layout_marginLeft="90dip" 
      android:background="@drawable/icon"> 

    </ImageView> 

    <ImageView 
      android:layout_width="52dip" 
      android:layout_height="32dip" 
      android:layout_alignParentLeft="true" 
      android:background="@drawable/btn_back_52x32"> 

    </ImageView> 


    <ImageButton 
      android:layout_height="34dip" 
      android:layout_width="64dip" 
      android:id="@+id/btn_header_what_search_user" 
      android:layout_marginLeft="0dip" 
android:layout_alignParentright="true" 
      android:background="@drawable/btn_nedit_nontext_64x34"> 
    </ImageButton> 
</RelativeLayout> 
0

另一種解決方案可能是使用佈局權重。設置總權重3,然後爲每個圖像視圖設置權​​重1。這樣佈局分成3部分,你應該能夠調整每個部分的重力。類似的東西:

<LinearLayout 
    ... 
    android:weightSum="3" > 

    <ImageView 
     ... 
     android:layout_width="0dp" 
     android:layout_weight="1" /> 
+0

什麼是做體重?我在想如果我想使用這個屏幕的百分比? – DuyguK 2012-02-28 13:13:21

+0

隨着體重的增加,您可以動態分配您的佈局,具體取決於您使用的因素。另一個使用不同因素的例子,如weightSum =「10」,一個imageView有5個,兩個textview兩個。只是玩弄它,但請記住將layout_width始終設置爲0dp,否則將無法工作...... – ramdroid 2012-02-28 14:14:23