2017-08-23 70 views
0

我的Android應用程序中有一個GridView,它具有圖像。我在GridView中有10行和2列。這樣,我在每個網格中都有相同大小的圖像,即20個圖像。Android GridView在較小和較大的屏幕設備上無法正常工作

網格代碼是,

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_margin="5dp" 
android:columnWidth="140dp" 
android:drawSelectorOnTop="true" 
android:gravity="center" 
android:numColumns="2" 
android:stretchMode="columnWidth" 
android:verticalSpacing="5dp" 
android:focusable="true" 
android:clickable="true" 
/> 

同時在Java代碼中,我使用的適配器。下面的代碼,這臺單獨的網格大小和圖像,與scaleType,

 imageView = new ImageView(mContext); 
     imageView.setLayoutParams(new GridView.LayoutParams(340, 400)); 
     imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imageView.setPadding(10, 10, 10, 10); 

它的正常工作的設備的屏幕尺寸5'0,但在其他設備,如5'7,-4'5,這是不以相同的方式工作。網格(和他們的個人圖像)有些隱藏/裁剪。即使在5'0以下,它也顯示出一些額外的空間,看起來很差。

GridView如何在不同的屏幕尺寸下正常工作。

下面是一個示例,它是如何看起來像在5'7,

5'7 screen size

下面是一個示例,它是如何看起來像在5'0。 這就是我希望它在每個屏幕尺寸上都可見的方式。

5'0 screen size

下面是一個示例,它是如何看起來像在4'0。這裏的圖像重疊。

4'0 screen size

+0

我最近在一個應用程序曾與圖像,並使用'RecyclerView'傳遞'GridLayout'作爲佈局管理器到'RecyclerView'和2的數細胞,甚至在平板電腦上看起來也很漂亮。 – Yupi

回答

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) { 

      //Calculation of ImageView Size - density independent. 
      //maybe you should do this calculation not exactly in this method but put is somewhere else. 
      Resources r = Resources.getSystem(); 
      float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, r.getDisplayMetrics()); 


      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams((int)px, (int)px)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      //imageView.setPadding(8, 8, 8, 8); 
      imageView.setBackgroundColor(Color.BLUE); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

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

來源: https://stackoverflow.com/a/11485625/7639113

加入上面的代碼後,我也爲更大的創造不同的佈局屏幕,像這樣:

normal_layout.xml

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_margin="5dp" 
android:numColumns="2" 
android:verticalSpacing="5dp" 
/> 

bigscreen_layout.xml

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_margin="5dp" 
android:numColumns="4" 
android:verticalSpacing="5dp" 
/> 

注意,我numColumns列設置爲不同高於佈局。

這裏是我創建的佈局,針對不同屏幕尺寸:

  1. 創建資源
新的Android資源目錄

  • 更改資源鍵入佈局,爲可用限定符選擇最小屏幕寬度,然後單擊「>>」
  • 填寫600在最小的屏幕寬度,Android Studio中會自動生成佈局sw600dp目錄。點擊確定。
  • 現在,所有你需要做的就是地方normal_layout.xml在佈局目錄,並將bigscreen_layout.xml在佈局sw600dp目錄。
  • 你可以看一下在不同的屏幕尺寸詳細的官方Android指南Supporting Multiple Screens在:

    1

    您可以使用項目視圖爲你的GridView一樣的ListView在適配器如下:

    public View getView(int position, View convertView, ViewGroup parent) { 
        ViewHolder holder; 
    
        if (convertView == null) { 
         convertView = LayoutInflater.from(mContext).inflate(R.layout.item_grid, null); 
    
         holder = new ViewHolder(); 
         holder.mImage = (ImageView) convertView.findViewById(R.id.img); 
         convertView.setTag(holder); 
        } else { 
         holder = (ViewHolder) convertView.getTag(); 
        } 
        holder.mImage.setImageResource(imageIDs[position]); 
        return convertView; 
    } 
    
    public class ViewHolder { 
        private ImageView mImage; 
    } 
    

    我已經創建item_grid如下:

    <?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="wrap_content" 
        android:orientation="vertical"> 
    
        <ImageView 
         android:id="@+id/grid_image" 
         android:layout_width="match_parent" 
         android:layout_height="130dp" /> 
    </LinearLayout> 
    

    建議:你可以用GridLayoutManager使用RecyclerView 2列

    相關問題