2012-01-02 123 views
19

我的應用程序的網格視圖有3行3列。無論設備的屏幕大小如何,我都希望它填滿屏幕。Android gridview調整到屏幕大小

我試着得到窗口大小並相應地設置了layoutparams。但是這並沒有給出完美的對齊方式,因爲它在linearlayout中與權重一起工作。

因此,我們可以使用權重爲gridview或有任何其他可用的android屬性。 非常感謝您的時間和迴應。

<GridView 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/gridview" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_below="@+id/text1view" 
       android:background="@drawable/home_bg" 
       android:gravity="center" 
       android:horizontalSpacing="10dip" 
       android:numColumns="3" 
       android:stretchMode="columnWidth" /> 

每個網格產品

<?xml version="1.0" encoding="utf-8"?> 

<ImageView 
    android:id="@+id/grid_item_image" 
    android:layout_width="wrap_content" 
    android:layout_height="75dip" 
    android:layout_margin="0dip" 
    android:padding="0dip" > 
</ImageView> 

<TextView 
    android:id="@+id/grid_item_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_below="@+id/grid_item_image" 
    android:gravity="center_horizontal" 
    android:padding="0dip" 
    android:textColor="#000000" 
    android:textSize="10sp" > 
</TextView> 

代碼的適配器的:

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 

    if (convertView == null) { 
     LayoutInflater mInflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = mInflater.inflate(R.layout.griditems, null); 
     holder = new ViewHolder(); 
     holder.text1 = (TextView) convertView 
       .findViewById(R.id.grid_item_text); 

     holder.image = (ImageView) convertView 
       .findViewById(R.id.grid_item_image); 

     // if it's not recycled, initialize some attributes 
     holder.image.setImageResource(gridItemIds[position]); 
     holder.text1.setText(gridTitles[position]); 
     int h = mContext.getResources().getDisplayMetrics().densityDpi; 

     // holder.image.setLayoutParams(new LayoutParams(h-50,h-50)); 
     convertView.setLayoutParams(new GridView.LayoutParams(h - 45, 
       h - 39)); 
     // holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     // holder.image.setScaleType(ImageView.ScaleType.FIT_XY); 
     // holder.image.setPadding(20, 20, 20, 20); 

     // convertView.setLayoutParams(new GridView.LayoutParams(Utils 
     // .getLayoutParameter(), Utils.getLayoutParameter()+50)); 

     holder.image.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       new ModuleManager().startModuleACtivity(position, mContext); 
      } 
     }); 
     convertView.setTag(holder); 
    } else { 

     holder = (ViewHolder) convertView.getTag(); 

    } 

    return convertView; 
} 

static class ViewHolder { 
    TextView text1; 
    ImageView image; 

} 

請看行

convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39)); 

我到了這個值與一些試驗在不同大小的模擬器。 但我不認爲它是一個正確的方式來定義高度和寬度。

+0

顯示一些代碼。你在想什麼? – TryTryAgain 2012-01-02 05:30:15

+0

我已添加。租賃看看。感謝您的時間和幫助 – png 2012-01-03 00:55:10

+0

同樣的問題..你能幫助我,你是如何脫離這個問題的。 – Sreeram 2012-04-25 11:20:03

回答

15

據我瞭解你的問題,因爲你還沒有發佈任何代碼,你想要做的是,你希望gridview出現在全屏幕上。

首先,你必須使用一些佈局,把你的GridView中執行以下操作:在佈局

製作的高度和寬度,以填補父:

android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

然後,設置高度和你的gridview的寬度也是fill_parent。

嗯,這只是一個假設的解決方案,你可能一直在用這種方式工作。放一些代碼會好很多。希望這對你有用。

更新: 那麼問題可能在於這裏:

convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39)); 

變化:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); 
convertView.setLayoutParams(new GridView.LayoutParams(params)); 

希望這對你的作品。

+0

我已經添加。租賃看看。感謝您的時間和幫助 – png 2012-01-03 00:51:55

+0

@ Preetha ...查看最新的答案。 – 2012-01-03 05:57:16

+0

其工作..非常感謝你 – png 2012-01-04 00:45:35

10

@preetha:首先看看這個例子,我用這個和我的佈局沒有影響,不管設計的高度和寬度....

http://developer.android.com/resources/tutorials/views/hello-gridview.html

設置XML以下

android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:columnWidth="90dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 

希望這有助於...

+1

謝謝你。我曾試過這個,不工作。對我而言,否:cols必須是三。另外我的網格是一個imageview + textview – png 2012-01-03 02:29:57

相關問題