最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中GridView插件的使用方法

 更新時間:2019年03月28日 11:03:55   作者:徐劉根  
今天小編就為大家分享一篇關(guān)于Android中GridView插件的使用方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" >
  <GridView
    android:id="@+id/gridView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="16dp"
    android:numColumns="4"
    android:stretchMode="columnWidth" >
  </GridView>
</RelativeLayout>

gridview_item.xml

這個是一個item的單元格樣式的,有圖片和文字

<?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" >
  <ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:scaleType="fitCenter"
    android:src="@drawable/ic_launcher" />
  <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="TextView" />
</LinearLayout>

MainActivity.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
public class MainActivity extends Activity {
  private GridView gridView;
  private GridViewAdapter adapter;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // 設(shè)置適配器的圖片資源
    int[] imageId = new int[] { R.drawable.chat_tool_camera,
        R.drawable.chat_tool_location, R.drawable.chat_tool_paint,
        R.drawable.chat_tool_video, R.drawable.chat_tool_voice,
        R.drawable.chat_tool_camera, R.drawable.chat_tool_location,
        R.drawable.chat_tool_paint, R.drawable.chat_tool_video,
        R.drawable.chat_tool_voice, R.drawable.chat_tool_camera,
        R.drawable.chat_tool_location, R.drawable.chat_tool_paint,
        R.drawable.chat_tool_video, R.drawable.chat_tool_voice,
        R.drawable.chat_tool_camera, R.drawable.chat_tool_location,
        R.drawable.chat_tool_paint, R.drawable.chat_tool_video,
        R.drawable.chat_tool_voice };
    // 設(shè)置標題
    String[] title = new String[] { "相機", "定位", "畫筆", "視頻", "聲音", "相機",
        "定位", "畫筆", "視頻", "聲音", "相機", "定位", "畫筆", "視頻", "聲音", "相機",
        "定位", "畫筆", "視頻", "聲音" };
    List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
    // 將上述資源轉(zhuǎn)化為list集合
    for (int i = 0; i < title.length; i++) {
      Map<String, Object> map = new HashMap<String, Object>();
      map.put("image", imageId[i]);
      map.put("title", title[i]);
      listitem.add(map);
    }
    adapter = new GridViewAdapter(MainActivity.this, listitem);
    gridView = (GridView) this.findViewById(R.id.gridView);
    gridView.setAdapter(adapter);
  }
}

GridViewAdapter.java

這個是適配器

import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GridViewAdapter extends BaseAdapter {
  private Context context;
  private List<Map<String, Object>> listitem;
  public GridViewAdapter(Context context,List<Map<String, Object>> listitem) {
    this.context = context;
    this.listitem = listitem;
  }
  @Override
  public int getCount() {
    return listitem.size();
  }
  @Override
  public Object getItem(int position) {
    return listitem.get(position);
  }
  @Override
  public long getItemId(int position) {
    return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = LayoutInflater.from(context).inflate(R.layout.gridview_item, null);
    }
    ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
    TextView textView = (TextView) convertView.findViewById(R.id.textView);
    Map<String, Object> map = listitem.get(position);
    imageView.setImageResource((Integer) map.get("image"));
    textView.setText(map.get("title") + "");
    return convertView;
  }
}

效果圖如下:

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • Android 動畫之AlphaAnimation應(yīng)用詳解

    Android 動畫之AlphaAnimation應(yīng)用詳解

    本節(jié)講解AlphaAnimation 動畫,窗口的動畫效果,淡入淡出什么的,有些游戲的歡迎動畫,logo的淡入淡出效果就使用AlphaAnimation,具體的祥看本文,需要的朋友可以參考下
    2012-12-12
  • 詳解Android中處理崩潰異常

    詳解Android中處理崩潰異常

    這篇文章主要介紹了Android中處理崩潰異常的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • Android 通過代碼安裝 APK的方法詳解

    Android 通過代碼安裝 APK的方法詳解

    這篇文章主要介紹了Android 通過代碼安裝 APK的方法詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)火鍋工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • Android webveiw 出現(xiàn)棧錯誤解決辦法

    Android webveiw 出現(xiàn)棧錯誤解決辦法

    這篇文章主要介紹了Android webveiw 出現(xiàn)棧錯誤解決辦法的相關(guān)資料,出現(xiàn)java.lang.UnsupportedOperationException: For security reasons, WebView is not allowed in privileged processes,這里提供解決辦法,需要的朋友可以參考下
    2017-08-08
  • Android中Item實現(xiàn)點擊水波紋效果

    Android中Item實現(xiàn)點擊水波紋效果

    這篇文章主要給大家介紹了關(guān)于Android中Item實現(xiàn)點擊水波紋效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Android Studio中導(dǎo)入module的方法(簡單版)

    Android Studio中導(dǎo)入module的方法(簡單版)

    這篇文章主要介紹了AndroidStudio中導(dǎo)入module的方法,本文是一篇簡易版的教程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-01-01
  • listview改變選中行的字體顏色實例介紹

    listview改變選中行的字體顏色實例介紹

    選中item,其字體設(shè)置為#3197FF,未選中的,其字體為#FFFFFF,下面與大家分享下listview怎么改變選中行字體顏色,感興趣的朋友可以參考下哈
    2013-06-06
  • ViewPager打造輪播圖Banner/引導(dǎo)頁Guide

    ViewPager打造輪播圖Banner/引導(dǎo)頁Guide

    這篇文章主要為大家詳細介紹了ViewPager打造輪播圖Banner和引導(dǎo)頁Guide,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android中ListView下拉刷新的實現(xiàn)方法

    Android中ListView下拉刷新的實現(xiàn)方法

    這篇文章主要為大家詳細介紹了Android中ListView下拉刷新的實現(xiàn)方法,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Android基于zxing的二維碼(網(wǎng)格)掃描 仿支付寶網(wǎng)格掃描

    Android基于zxing的二維碼(網(wǎng)格)掃描 仿支付寶網(wǎng)格掃描

    這篇文章主要為大家詳細介紹了Android基于zxing的二維碼網(wǎng)格掃描,仿支付寶網(wǎng)格掃描,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03

最新評論

乌恰县| 十堰市| 古丈县| 云浮市| 宿迁市| 江华| 北宁市| 镇雄县| 巩义市| 孝昌县| 阿拉尔市| 广州市| 麻阳| 武冈市| 汕尾市| 朔州市| 桐城市| 涞源县| 龙游县| 耿马| 和政县| 习水县| 建水县| 安多县| 芦山县| 东宁县| 洛扎县| 罗城| 盱眙县| 轮台县| 新巴尔虎左旗| 宿州市| 大荔县| 大余县| 定州市| 烟台市| 祥云县| 出国| 永新县| 南汇区| 长汀县|