2017-10-09 46 views
0

我是android新手。我想根據總的行和列構建一個網格,並在視圖中顯示。創建網格後,我需要將圖標放置在網格之一中。該圖標將來自服務器URL。網格應該如下所示。 enter image description here如何在Android中動態使用Total行和列來創建網格

創建的網格應該適合屏幕。我怎麼做?有人可以給我一個代碼示例。提前致謝!!!

回答

1

RES /佈局/ gridview_android_example_with_image.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_android_example" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:columnWidth="100dp" 
     android:gravity="center" 
     android:minHeight="90dp" 
     android:numColumns="auto_fit" 
     android:stretchMode="columnWidth" /> 

</LinearLayout> 

AndroidGridViewDisplayImages.java

import android.content.Context; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class AndroidGridViewDisplayImages extends AppCompatActivity { 

    GridView androidGridView; 


// Dummy Array of images (Replace with your own values) 
    Integer[] imageIDs = { 
      R.drawable.email, R.drawable.mobile, R.drawable.alram, 
      R.drawable.android, R.drawable.wordpress, R.drawable.web, 
      R.drawable.email, R.drawable.mobile, R.drawable.alram, 
      R.drawable.android, R.drawable.wordpress, R.drawable.web, 
      R.drawable.email, R.drawable.mobile, R.drawable.alram, 
      R.drawable.android, R.drawable.wordpress, R.drawable.web, 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.gridview_android_example_with_image); 

     androidGridView = (GridView) findViewById(R.id.gridview_android_example); 
     androidGridView.setAdapter(new ImageAdapterGridView(this)); 

     androidGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, 
            View v, int position, long id) { 
       Toast.makeText(getBaseContext(), "Grid Item " + (position + 1) + " Selected", Toast.LENGTH_LONG).show(); 
      } 
     }); 

    } 

    public class ImageAdapterGridView extends BaseAdapter { 
     private Context mContext; 

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

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

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

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

     public View getView(int position, View convertView, ViewGroup parent) { 
      ImageView mImageView; 

      if (convertView == null) { 
       mImageView = new ImageView(mContext); 
       mImageView.setLayoutParams(new GridView.LayoutParams(130, 130)); 
       mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
       mImageView.setPadding(16, 16, 16, 16); 
      } else { 
       mImageView = (ImageView) convertView; 
      } 
      mImageView.setImageResource(imageIDs[position]); 
      return mImageView; 
     } 
    } 
} 
+0

感謝喲這麼多@Akshay Chopde。我不想在網格中顯示圖像。我只需要在網格中放置一個圖標。/*整數[] imageIDs = {} * /這是我的情況下的空數組。如果沒有這個,我如何構建網格?它會起作用嗎? –

+0

您需要包含該組圖像的數據列表。這樣您就可以迭代到列表的大小,並將列表中的每個圖像設置爲網格的單元格。 –