亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android中使用GridView進行應(yīng)用程序UI布局的教程

 更新時間:2016年06月08日 16:18:45   作者:wizardforcel  
GridView即平常我們見到的類似九宮格的矩陣型布局,只不過默認不帶分割線,這里我們就從基礎(chǔ)開始來看一下Android中使用GridView進行應(yīng)用程序UI布局的教程

0.簡介
GridView 和 ListView 有共同的父類:AbsListView,因此 GridView 和 ListView 具有一定 的相似性。GridView與ListView的主要區(qū)別在于:ListView只是在一個方向上分布;而 GridView則會在兩個方向上分布。
與ListView類似的是,GridView也需要通過Adapter來提供顯示的數(shù)據(jù):開發(fā)者既可通 過SimpleAdapter來為GridView提供數(shù)據(jù),也可通過開發(fā) BaseAdaptei的子類來為GridView 提供數(shù)據(jù)。不管使用哪種方式,GridView與ListView的用法基本是一致的。

1.相關(guān)屬性:
下面是GridView中的一些屬性:
(1)android:columnWidth:設(shè)置列的寬度
(2)android:gravity:組件對其方式
(3)android:horizontalSpacing:水平方向每個單元格的間距
(4)android:verticalSpacing:垂直方向每個單元格的間距
(5)android:numColumns:設(shè)置列數(shù)
(6)android:stretchMode:設(shè)置拉伸模式,可選值如下: none:不拉伸;spacingWidth:拉伸元素間的間隔空隙 columnWidth:僅僅拉伸表格元素自身 spacingWidthUniform:既拉元素間距又拉伸他們之間的間隔空襲

2.使用示例:
下面通過一個簡單的例子來熟悉這個控件的使用: (這里用的Adapter我們直接用之2.5.0中教大家寫的可復(fù)用的BaseAdapter~)
實現(xiàn)的效果圖:

代碼實現(xiàn):
首先是GridView 的 Item的布局:item_grid_icon.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="5dp">

  <ImageView
    android:id="@+id/img_icon"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:layout_centerInParent="true"
    android:src="@mipmap/iv_icon_1" />

  <TextView
    android:id="@+id/txt_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/img_icon"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:text="呵呵"
    android:textSize="18sp" />

</RelativeLayout>

接著我們寫個entity實體類:Icon.java:

public class Icon {
  private int iId;
  private String iName;

  public Icon() {
  }

  public Icon(int iId, String iName) {
    this.iId = iId;
    this.iName = iName;
  }

  public int getiId() {
    return iId;
  }

  public String getiName() {
    return iName;
  }

  public void setiId(int iId) {
    this.iId = iId;
  }

  public void setiName(String iName) {
    this.iName = iName;
  }
}

最后是MainActivity的布局以及Java代碼
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:padding="5dp"
  tools:context=".MainActivity">

  <!--numColumns設(shè)置每行顯示多少個-->
  <GridView
    android:id="@+id/grid_photo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="3" />

</RelativeLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity {

  private Context mContext;
  private GridView grid_photo;
  private BaseAdapter mAdapter = null;
  private ArrayList<Icon> mData = null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContext = MainActivity.this;
    grid_photo = (GridView) findViewById(R.id.grid_photo);

    mData = new ArrayList<Icon>();
    mData.add(new Icon(R.mipmap.iv_icon_1, "圖標(biāo)1"));
    mData.add(new Icon(R.mipmap.iv_icon_2, "圖標(biāo)2"));
    mData.add(new Icon(R.mipmap.iv_icon_3, "圖標(biāo)3"));
    mData.add(new Icon(R.mipmap.iv_icon_4, "圖標(biāo)4"));
    mData.add(new Icon(R.mipmap.iv_icon_5, "圖標(biāo)5"));
    mData.add(new Icon(R.mipmap.iv_icon_6, "圖標(biāo)6"));
    mData.add(new Icon(R.mipmap.iv_icon_7, "圖標(biāo)7"));

    mAdapter = new MyAdapter<Icon>(mData, R.layout.item_grid_icon) {
      @Override
      public void bindView(ViewHolder holder, Icon obj) {
        holder.setImageResource(R.id.img_icon, obj.getiId());
        holder.setText(R.id.txt_icon, obj.getiName());
      }
    };

    grid_photo.setAdapter(mAdapter);

    grid_photo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(mContext, "你點擊了~" + position + "~項", Toast.LENGTH_SHORT).show();
      }
    });

  }

}

相關(guān)文章

  • Android實戰(zhàn)項目之實現(xiàn)一個簡單計算器

    Android實戰(zhàn)項目之實現(xiàn)一個簡單計算器

    隨著移動互聯(lián)網(wǎng)的普及,手機應(yīng)用程序已經(jīng)成為人們生活中不可或缺的一部分,計算器是一類被廣泛使用的應(yīng)用程序之一,這篇文章主要給大家介紹了關(guān)于Android實戰(zhàn)項目之實現(xiàn)一個簡單計算器的相關(guān)資料,需要的朋友可以參考下
    2023-10-10
  • PowerManagerService之自動滅屏流程解析

    PowerManagerService之自動滅屏流程解析

    這篇文章主要為大家介紹了PowerManagerService之自動滅屏流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Android使用網(wǎng)絡(luò)獲取定位的方法

    Android使用網(wǎng)絡(luò)獲取定位的方法

    這篇文章主要為大家詳細介紹了Android使用網(wǎng)絡(luò)獲取定位的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Android開發(fā)筆記之:一分鐘學(xué)會使用Logcat調(diào)試程序的詳解

    Android開發(fā)筆記之:一分鐘學(xué)會使用Logcat調(diào)試程序的詳解

    本篇文章是對Android中Logcat調(diào)試程序的使用進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • Kotlin集合List Set Map使用介紹詳解

    Kotlin集合List Set Map使用介紹詳解

    集合是可變數(shù)量(可能為0)的一組條目,kotlin標(biāo)準(zhǔn)庫提供一個整套用于集合管理的工具,各種集合對于解決問題都具有重要意義,并且經(jīng)常用到。kotlin中的集合與Java基本類似
    2022-09-09
  • Android中TabLayout結(jié)合ViewPager實現(xiàn)頁面切換

    Android中TabLayout結(jié)合ViewPager實現(xiàn)頁面切換

    這篇文章主要為大家詳細介紹了Android中TabLayout結(jié)合ViewPager實現(xiàn)頁面切換效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android編程鬧鐘設(shè)置方法詳解

    Android編程鬧鐘設(shè)置方法詳解

    這篇文章主要介紹了Android編程鬧鐘設(shè)置方法,結(jié)合實例形式分析了Android鬧鐘設(shè)置的步驟與時間監(jiān)聽的操作技巧,需要的朋友可以參考下
    2016-10-10
  • Android實現(xiàn)底部導(dǎo)航欄效果

    Android實現(xiàn)底部導(dǎo)航欄效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)底部導(dǎo)航欄效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • OkHttp攔截器在Android網(wǎng)絡(luò)中的使用和工作原理

    OkHttp攔截器在Android網(wǎng)絡(luò)中的使用和工作原理

    當(dāng)涉及到Android應(yīng)用程序中的網(wǎng)絡(luò)請求處理時,OkHttp是一個非常強大和流行的工具,其中一個關(guān)鍵的功能是攔截器,在本文中,我們將深入研究OkHttp攔截器,了解其工作原理以及如何使用它們來優(yōu)化您的Android應(yīng)用程序,需要的朋友可以參考下
    2023-09-09
  • Android中使用Kotlin實現(xiàn)一個簡單的登錄界面

    Android中使用Kotlin實現(xiàn)一個簡單的登錄界面

    Kotlin 是一種在 Java 虛擬機上運行的靜態(tài)類型編程語言,被稱之為 Android 世界的Swift,由 JetBrains 設(shè)計開發(fā)并開源。接下來本文通過實例代碼給大家講解Android中使用Kotlin實現(xiàn)一個簡單的登錄界面,一起看看吧
    2017-09-09

最新評論