为什么我的GridView只显示一列?

时间:2012-12-20 12:06:52

标签: android gridview

我不明白为什么我在网格视图中只得到1列,无论我做什么......令人沮丧。

活动的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;
    }

2 个答案:

答案 0 :(得分:5)

嗨这是我的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代码:

<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列。让我试试这个属性..

答案 1 :(得分:0)

也许您的图片太宽,无法在屏幕上的两列中进行?

BTW每次创建视图都非常昂贵(内存) - 看看ViewHolder模式。