2017-08-29 134 views
0

我想在我的應用程序集圖片瀏覽的高度和寬度在網格視圖

我使用網格視圖來顯示圖像來創建一個圖片庫,但圖像來顯示搞砸

我怎麼可以顯示圖像平等的嗎?如其他圖像庫應用

佈局文件:

<GridView 
     android:id="@+id/galleryGridView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:numColumns="3" 
     android:rowCount="3" 
     android:stretchMode="columnWidth" 
     android:verticalSpacing="0dp" 
     android:horizontalSpacing="0dp" /> 

Java文件:

public class GalleryManager extends BaseAdapter { 
    Context context; 
    List<Drawable> list; 

    public GalleryManager(Context context , List<Drawable> list) { 
     this.context = context; 
     //this.list = list; 
     this.list = list; 
    } 

    @Override 
    public int getCount() { 
     return list.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     try { 
      ImageView imageView = new ImageView(context); 
      imageView.setImageDrawable(list.get(position)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER); 
      return imageView; 
     } 
      catch(Exception e){ 
       Log.i("Tag", "getView: "+e.toString()); 
      } 
      return null; 
    } 
} 

回答

2

嘗試

public class MyImageView extends android.support.v7.widget.AppCompatImageView { 
public MyImageView(Context context) { 
    super(context); 
} 

public MyImageView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    // super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    int size = getContext().getResources().getDimensionPixelSize(R.dimen.space); 
    setMeasuredDimension(size, size); 
} 
} 

在dimen.xml

<dimen name="space">100dp</dimen> 

而且

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    try { 
     MyImageView imageView = new MyImageView(context); 
     imageView.setImageDrawable(list.get(position)); 
     imageView.setScaleType(ImageView.ScaleType.CENTER); 
     return imageView; 
    } 
     catch(Exception e){ 
      Log.i("Tag", "getView: "+e.toString()); 
     } 
     return null; 
} 
} 
+0

得到這個錯誤:這個自定義視圖應該擴展android.support.v7.widget.app CompatImageView ... – lasthope

+0

我更新我的答案 –

0

嘗試在你的java代碼圖像權重設置爲1

相關問題