2011-08-20 62 views
3

我試圖在平板電腦上佈局相對佈局中的視圖,非常像書架。將會有這麼多,然後創建一個新的行。以編程方式在佈局中佈置子視圖

我的初始狀態是這樣的:

<ScrollView 
    android:layout_marginTop="150sp" 
    android:layout_marginLeft="50sp" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <RelativeLayout 
     android:id="@+id/user_list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <!-- Add User Avatar --> 
     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
      <ImageView 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:src="@drawable/user_frame" /> 
      <ImageView 
       android:id="@+id/demo_image" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:src="@drawable/add_user" 
       android:layout_marginLeft="10px" /> 
      <ImageView 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:src="@drawable/user_name_background" 
       android:layout_marginLeft="10px" /> 
      <TextView 
       android:id="@+id/user_name" 
       android:layout_width="180px" 
       android:layout_height="wrap_content" 
       android:textSize="20sp" 
       android:textStyle="bold" 
       android:gravity="center_horizontal" 
       android:text="Add New User" /> 
     </RelativeLayout> 

     </RelativeLayout> 
    </ScrollView> 

我取的記錄從數據庫中,併爲每個記錄我需要放置一個版本的「添加用戶阿凡達」部分的由上述可見。我想做5,然後換行到新行。

我最初的想法是使用佈局inflater並以編程方式添加視圖,使用RelativeLayout.LayoutParams來設置layout_toRightOf值。

雖然必須有一個更簡單的方法。我見過很多使用「書架」隱喻的應用程序。

+0

我的第一個多達五個鏡像視圖的水平佈局以爲會使用GridView。 – kcoppock

回答

3

有幾個選項我可以想到。

  1. 而不是使用相對佈局,爲什麼不使用LinearLayouts水平屬性?
  2. 使用TableLayout,很像的LinearLayout
  3. 使用帶有自定義適配器的ListView填補五「添加用戶阿凡達」每ListRow
3

編程鋪出的觀點是相當簡單的。我爲我所有的活動/觀點都這樣做。我喜歡Android的XML佈局概念,但發現一旦視圖根據外部數據(例如數據庫)完全動態變化,就會變得太複雜。

我選擇我最外層的佈局,然後只在XML中實例化該佈局。然後在我的活動中,我使用普通的find by id call找到外部佈局,然後使用一系列add(View)方法調用來根據需要添加我的動態創建的視圖或佈局。

您需要考慮不同的屏幕方向,像素密度和尺寸。獨立於設備的像素將成爲您的朋友。例如,要創建圖像視圖,您需要加載位圖,找出需要調整大小的位置(如果需要),然後將其設置爲新ImageView實例的drawable,然後將其添加到佈局。

聽起來像你外佈局將與內垂直線性佈局的滾動視圖中,然後對每一個「架子」是然後對每個「書」

相關問題