2

我不明白为什么无论我做什么,我的网格视图中都只有一列......令人沮丧。

活动的xml

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

  <GridView
      android:id="@+id/gridView_album"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:numColumns="2"
      android:verticalSpacing="10dp"
      android:horizontalSpacing="10dp"
      android:stretchMode="columnWidth"
      android:gravity="center" />

</LinearLayout>

对于网格视图中的每个项目(这在我的适配器中被夸大了)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical" >

  <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="5dp"
      android:layout_marginRight="5dp"
      android:orientation="vertical" >

    <TextView
        android:id="@+id/textView_albumTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="album title" />

  </LinearLayout>

  <ImageView
      android:id="@+id/imageView1"
      android:layout_width="105dp"
      android:layout_height="100dp"
      android:src="@drawable/abs__ab_share_pack_holo_light" />

  <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="20dp"
        android:layout_height="20dp"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"      
        android:src="@drawable/abs__ab_solid_light_holo" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="20dp"
        android:layout_height="20dp"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"      
        android:src="@drawable/abs__ab_solid_light_holo" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="20dp"
        android:layout_height="20dp"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"      
        android:src="@drawable/abs__ab_solid_light_holo" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="20dp"
        android:layout_height="20dp"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"      
        android:src="@drawable/abs__ab_solid_light_holo" />

  </LinearLayout>

</LinearLayout>

和我的适配器的 getView

   @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        JsonAlbum jsonAlbum = (JsonAlbum) getItem(position);
        ArrayList<JsonAlbumImage> jsonAlbumImageArray = jsonAlbum.album_image_list;

        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.album_list_row, null);

        }


        TextView textView_albumTitle = (TextView) v.findViewById(R.id.textView_albumTitle);
        textView_albumTitle.setText(jsonAlbum.title);

        ImageView imageView1 = (ImageView)v.findViewById(R.id.imageView1);
        ImageView imageView2 = (ImageView)v.findViewById(R.id.imageView2);
        ImageView imageView3 = (ImageView)v.findViewById(R.id.imageView3);
        ImageView imageView4 = (ImageView)v.findViewById(R.id.imageView4);
        ImageView imageView5 = (ImageView)v.findViewById(R.id.imageView5);

        ArrayList<ImageView> imageViewList = new ArrayList<ImageView>();
        imageViewList.add(imageView1);
        imageViewList.add(imageView2);
        imageViewList.add(imageView3);
        imageViewList.add(imageView4);
        imageViewList.add(imageView5);

        int imageCount = Math.min( imageViewList.size(), jsonAlbumImageArray.size());
        for(int i=0; i < imageCount; ++i)
        {
            ImageView imageView = imageViewList.get(i);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            JsonAlbumImage jsonAlbumImage = jsonAlbumImageArray.get(i);
            imageLoader.displayImage(jsonAlbumImage.url_image, imageView, options);
        }

        Log.i("debug", "width: "+ Integer.toString(v.getWidth()));
        return v;
    }
4

2 回答 2

6

嗨这是我的Gridview代码

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView1"
    android:numColumns="2"
    android:gravity="center"
    android:columnWidth="100dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

我的图像视图代码:

<ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="150px"
        android:layout_height="150px"
        android:layout_marginRight="50px"
        android:src="@drawable/ic_launcher" >
    </ImageView>

我得到 2 列..让你试试这个属性..

于 2012-12-20T12:21:10.717 回答
0

也许您的图像太宽而无法在屏幕上的两列中显示?

顺便说一句,每次创建视图都非常昂贵(内存) - 看看 ViewHolder 模式。

于 2012-12-20T12:16:54.560 回答