2011-04-18 202 views

回答

4

最後我才得以實現的Android horizo​​ntall滾動網格視圖...這裏是代碼:

public class HorizontalGridView extends Activity { 
    Bitmap viewBgrnd; 
    ImageButton img ; 
    Button button; 
    Button b1[] = new Button[100]; 
    static int total=0; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     horizontalScrollGalleryLayout(); 
    } 

    public void horizontalScrollGalleryLayout() { 
     Integer[] mThumbIds = { 
      R.drawable.image0,R.drawable.image1,R.drawable.image2, 
      R.drawable.image3,R.drawable.image4,R.drawable.image5, 
      R.drawable.image6,R.drawable.image7,R.drawable.image8, 
      R.drawable.image9,R.drawable.image10,R.drawable.image11, 
      R.drawable.image12,R.drawable.image13,R.drawable.image14, 
      R.drawable.image15,R.drawable.image16,R.drawable.image17, 
      R.drawable.image18,R.drawable.image19,R.drawable.image20, 
      R.drawable.image21,R.drawable.image22,R.drawable.image23, 
      R.drawable.image24,R.drawable.image25,R.drawable.image26, 
      R.drawable.image27,R.drawable.image28,R.drawable.image29, 
      R.drawable.image30,R.drawable.image31,R.drawable.image32, 
      R.drawable.image33,R.drawable.image34,R.drawable.image35, 
      R.drawable.image36,R.drawable.image37,R.drawable.image38, 
      R.drawable.image39,R.drawable.image40,R.drawable.image41, 
      R.drawable.image42,R.drawable.image43,R.drawable.image44, 
      R.drawable.image45,R.drawable.image46,R.drawable.image47, 
      R.drawable.image48,R.drawable.image49,R.drawable.image50, 
      R.drawable.image51,R.drawable.image52,R.drawable.image53, 
      R.drawable.image54,R.drawable.image55,R.drawable.image56, 
      R.drawable.image57,R.drawable.image58,R.drawable.image59, 
      R.drawable.image60,R.drawable.image61,R.drawable.image62, 
      R.drawable.image63,R.drawable.image64,R.drawable.image65, 
      R.drawable.image66,R.drawable.image67,R.drawable.image68, 
      R.drawable.image69,R.drawable.image70,R.drawable.image71, 
      R.drawable.image72,R.drawable.image73,R.drawable.image74, 
      R.drawable.image75,R.drawable.image76,R.drawable.image77, 
      R.drawable.image77,R.drawable.image79,R.drawable.image8, 
      R.drawable.image81,R.drawable.image82,R.drawable.image83, 
      R.drawable.image84,R.drawable.image85,R.drawable.image86, 
      R.drawable.image87,R.drawable.image88,R.drawable.image89, 
      R.drawable.image90,R.drawable.image91,R.drawable.image92, 
      R.drawable.image93,R.drawable.image94,R.drawable.image95, 
      R.drawable.image96,R.drawable.image97,R.drawable.image98, 
      R.drawable.image99, 
     }; 
     HorizontalScrollView sv = new HorizontalScrollView(this); 
     LinearLayout llh = new LinearLayout(this); 
     llh.setOrientation(LinearLayout.HORIZONTAL); 
     LinearLayout.LayoutParams layoutParamsTV = new LinearLayout.LayoutParams(100,100); 
     LinearLayout.LayoutParams layoutParamsLL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     LinearLayout.LayoutParams layoutParamsLLD = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); 

     for(int k=0;k<mThumbIds.length;k++) 
     { 
      LinearLayout llv = new LinearLayout(this); 
      for (int i=0; i<4; i++) { 
       llv.setOrientation(LinearLayout.VERTICAL); 
       button = new Button(this); 
       try { 
        button.setBackgroundResource(mThumbIds[total]); 
        total++; 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       llv.addView(button, layoutParamsTV); 
       //b1[k].setBackgroundResource(mThumbIds[k]); 
      } 

      //llv.addView(button, layoutParamsTV); 
      llh.addView(llv, layoutParamsLL); 
      llh.setBackgroundColor(Color.GRAY); 
     } 

     sv.addView(llh, layoutParamsLLD); 
     setContentView(sv); 
     sv.setBackgroundColor(Color.GRAY); 
    } 
} 
+1

當然,小型集合可以接受,但請不要將其稱爲GridView。它缺少一個適配器,它可以讓你無需花費幾秒鐘就可以顯示所有視圖,並且可以一次填充數百個Bitmaps。 – kaay 2012-04-05 14:32:28

-1
+0

我看不到我想要的結果。其實我有一個網格視圖,它是垂直滾動,但我希望它水平滾動。 – Piyush 2011-04-18 11:10:58

+0

其實我發現在網格視圖中實現水平滾動是不可能的。相反,我們可以在水平滾動視圖中實現相對佈局,並將它們保留在它下面。 – Piyush 2011-04-19 05:05:31

+0

很好。我建議你自己回答你的問題,然後刪除我的答案。 – 2011-04-19 09:52:28

-1

在網格視圖問題的水平滾動條在這裏通過另一種方式解決。在水平滾動視圖中的每個網格視圖中都使用Thare按鈕。