2010-06-21 40 views
1

我以爲我已經理解Android的XML佈局,但似乎我沒有。 我想在一個屏幕上有四個ImageViews,可以同時查看四個圖像。Android XML佈局 - 在一個屏幕上的4個ImageView

它應該是這樣的,其中A至d表示圖像的觀點:

DDDAAA 
DDDAAA 
BBBCCC 
BBBCCC 

我怎麼能這樣做?我試過這種方式,但它不起作用。 Eclipse編輯器似乎對此也不太好。有更好的創建這些佈局嗎?

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TableRow> 
<ImageView android:id="@+id/ImageView01" android:layout_height="wrap_content" android:layout_width="wrap_content" ></ImageView> 
<ImageView android:id="@+id/ImageView02" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView> 
    </TableRow> 

    <TableRow> 
<ImageView android:id="@+id/ImageView03" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView> 
<ImageView android:id="@+id/ImageView04" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView> 
    </TableRow> 
</RelativeLayout> 

回答

3

什麼:

  • 一個LinearLayout,與vertical方向,這將包含一切
  • ,然後,第一LinearLayout,與horizontal方向
    • 將包含d和A,
  • 和同一種LinearLayouthorizontal取向,第二時間,對於B和C

並採用

  • android:layout_weight
  • android:gravity="center"

所以每個佈局需要足夠的地方和居中?


像這樣的位,我會說:

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

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:layout_weight="1" 
     > 

     <LinearLayout 
      android:id="@+id/DashboardNewPost" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" 
      android:layout_weight="1" 
      android:gravity="center" 
      > 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/new_post" 
      /> 
     </LinearLayout> 
     <LinearLayout 
      android:id="@+id/DashboardPostsList" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" 
      android:layout_weight="1" 
      android:gravity="center" 
      > 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/posts_list" 
      /> 
     </LinearLayout> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:layout_weight="1" 
     > 
     <LinearLayout 
      android:id="@+id/DashboardCommentsList" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" 
      android:layout_weight="1" 
      android:gravity="center" 
      > 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/comments_list" 
      /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/DashboardBlogParameters" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" 
      android:layout_weight="1" 
      android:gravity="center" 
      > 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/blog_params" 
      /> 
     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 


我想(沒試過),你甚至可以刪除LinearLayout這只是以防萬一每個ImageView,增加的屬性也必須改爲ImageView s ...

(在我從這個應用程序中拿走了這個,我不得不添加那些LinearLayout因爲不止有那些圖像)

+0

很酷!這正是我所尋找的。我不知道LinearLayout的垂直和水平選項,因爲我使用的編輯器很難在我看來使用。非常感謝:) – Nils 2010-06-21 19:48:39

+0

不客氣:-)玩得開心! – 2010-06-21 19:49:14

1

在RelativeLayout中使TableRows沒有意義。它可以幫助您閱讀介紹性的佈局documentation。它解釋了不同的佈局類型和它們的用法。

對於您的情況,您應該能夠將RelativeLayout更改爲TableLayout。

另外,如果您問佈局問題,如果您解釋您在嘗試時看到的行爲,那麼它很有幫助,而不僅僅是說它不起作用。