2014-01-08 96 views
-1

我想在一個LinearLayout中對齊3張圖片,以便一個頂部居中,另一個底部對齊左側,最後一個位於底部與右側對齊。非常像金字塔。我怎麼能這樣做?你能提供一個例子嗎?對齊圖片頂部中心,右下角和左下角

X 
X X 

回答

2

使用這一個:

<?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="vertical" > 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:gravity="center" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:src="@drawable/ic_launcher" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="bottom" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="right" > 

     <ImageView 
      android:id="@+id/imageView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 
    </LinearLayout> 

</LinearLayout> 
+0

有什麼辦法來控制頂部和底部圖像之間的間距?當我以這種方式或使用RelativeLayout進行操作時,頂部圖像和底部兩個圖像之間的間距是整個屏幕。 – Zack

+0

不,因爲我已經把它設置在底部。否則,如果您更改最後一秒佈局的高度以包裝內容,那麼如果顯示正確且沒有空格。 – Piyush

+0

@Zack你是WelCome!很高興幫助你。 – Piyush

1

你需要一個RelativeLayout的,而不是一個LinearLayout中。

圖片A必須指定屬性

android:layout_alignParentTop="true". 

圖像B必須指定屬性

android:layout_alignParentBottom="true" 

android:layout_alignParentLeft="true". 

圖像C必須指定屬性

android:layout_alignParentBottom="true" 

android:layout_alignParentRight="true" 
+0

@Zack:感謝您的糾正。對,我總是忘記佈局_...我在記憶的同時,也在爲我的項目工作。 ;) –

+0

沒問題。這個答案也是正確的,但使用RelativeLayout。謝謝! – Zack

1

這將是,如果你做到這一點在RelativeLayout的,但如果你想這樣做的的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="vertical" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top|center_horizontal" 
     android:src="@drawable/ic_launcher" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="bottom" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_launcher" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="right" > 

      <ImageView 
       android:id="@+id/imageView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ic_launcher" /> 
     </LinearLayout> 
    </LinearLayout> 

</LinearLayout> 
相關問題