2016-08-02 54 views
0

我想爲gridview設置3:2的比率(每個單元格的IE 300像素數爲200)。這裏是我的形象適配器:如何在Android網格視圖中設置自定義縱橫比?

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 

    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    public int getCount() { 
     return mThumbIds.length; 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if (convertView == null) { 
      // if it's not recycled, initialize some attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(200, 200)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

     imageView.setImageResource(mThumbIds[position]); 
     return imageView; 
    } 

    // references to our images 
    private Integer[] mThumbIds = { 
      R.drawable.image1, R.drawable.image2, R.drawable.image3, 
    }; 
} 

我試着調整imageView.setLayoutParams(new GridView.LayoutParams(200, 200));寬度參數300,但它會導致重疊。這裏是我的XML文件:

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:columnWidth="90dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
    /> 

古怪的重疊: enter image description here

+0

好心的迴應,我的回答有幫助嗎? –

+0

它沒有工作沒有,對不起,在迴應之前看到了圖書館。 – Brandex07

回答

0

您好,您可以嘗試像這樣從你的活動:

ViewGroup.LayoutParams layoutParams = gridview.getLayoutParams(); 
layoutParams.height = 200; 
layoutParams.width = 300; 
gridview.setLayoutParams(layoutParams); 

int final width = 300; 
int final height = 200; 
ViewGroup.LayoutParams lauoutParams = new ViewGroup.LayoutParams(width,height); 
gridView.setLayoutParams(lp); 

這可能有幫助。

+0

在ImageAdapter活動中? – Brandex07

+0

否您在呼叫適配器的主要活動。你在onCreate()裏初始化了gridView和其他視圖 –

相關問題