0

我有一個空白的線性列表,我有一個圖像列表,將動態加載(從網址,工作)。沒有圖像適配器的Android Gridview

我需要在網格中顯示圖像(例如當您訪問Facebook照片庫時)。

由於顯而易見的原因,我不能使用XML,我已經得到了顯示圖像的圖像,只是它們彼此相疊而且非常小。

也有可能將它們設置爲屏幕大小的百分比容易? (所以列表看起來一樣的所有設備)

這裏是到目前爲止的代碼:

private void Display() 
{ 
    Toast toast = Toast.makeText(getApplicationContext(), "Downloading Photos", Toast.LENGTH_SHORT); 
    toast.show(); 

    //Get width of the image. 
    int imageWidth = (int)getWindowManager().getDefaultDisplay().getWidth()/3; 
    LayoutParams gp = new GridView.LayoutParams(GridView.LayoutParams.FILL_PARENT, (int)imageWidth); 

    //Placeholders 
    for(int _i = 0; _i < _noOfPhotos; _i++) 
    { 
     ImageView imageView = new ImageView(getApplicationContext()); 

     imageView.setImageResource(R.drawable.no_image); 
     imageView.setLayoutParams(gp); 

     _imageViewArray.add(imageView); 
     this.addContentView(_imageViewArray.get(_i), gp); 
    } 

    //Set URL 

    //UrlImageViewHelper.setUrlDrawable(imageView, _userGallery.get(_i).getPicture()); 
} 

回答

1

使用GridView,這正是這個用例。

<GridView 
    android:id="@+id/content" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginTop="20dip" 
    android:layout_marginBottom="20dip" 
    android:numColumns="3" /> 

你可以用

float width = getWindowManager().getDefaultDisplay().getWidth()/3; 
mLayoutParams = new GridView.LayoutParams(
      GridView.LayoutParams.FILL_PARENT, 
      (int)width); // set the width as height for quadratic images 

獲取每個ImageView的寬度,然後,在你的ListAdapter爲GridView

public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView imageView = new ImageView(context); // need to check convertView 
    imageView.setImageResource(mImages.get(position)); 
    imageView.setLayoutParams(mLayoutParams); 

    return imageView; 
} 
+0

感謝您的回覆,但我得到的錯誤:錯誤:解析XML時出錯:第一個xml位上的未綁定前綴: -/ – 2012-07-13 19:43:47

+0

此xml片段需要添加到LinearLayout中。如果你想要我們自己添加android名稱空間屬性xmlns:android =「http://schemas.android.com/apk/res/android」 – SimonSays 2012-07-13 19:45:37

+0

它不那麼複雜。你只需要習慣ListAdapters的原則。這在android開發中非常常用(也適用於所有的ListViews)。這裏是一個GridView的簡單例子http://developer.android.com/guide/topics/ui/layout/gridview.html – SimonSays 2012-07-13 20:11:33

相關問題