博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android GridView 二维布局界面
阅读量:6088 次
发布时间:2019-06-20

本文共 2658 字,大约阅读时间需要 8 分钟。

GridView用于在界面上按行、列分布的方式来显示多个组件。
<
LinearLayout
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
=
"com.example.gridview.MainActivity"
>
   
<
GridView
       
android:id
=
"@+id/gridview"
       
android:layout_width
=
"wrap_content"
       
android:layout_height
=
"wrap_content"
       
android:numColumns
=
"auto_fit"
    //每行显示多少列     auto_fit 自动适应
       
android:horizontalSpacing
=
"10dp"
 //两列之间的间距
       
android:verticalSpacing
=
"10dp"
   //两行之间的间距
   
       
></
GridView
>
</
LinearLayout
>
 
 
<?
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_height
=
"wrap_content"
          
android:layout_width
=
"wrap_content"
          
android:background
=
"@drawable/ic_launcher"
          
/>
       
<
TextView
          
android:id
=
"@+id/text"
          
android:layout_height
=
"wrap_content"
          
android:layout_width
=
"wrap_content"
          
android:hint
=
"@string/hello_world"
          
/>
   
</
LinearLayout
>
 
public
class
MainActivity
extends
Activity
implements
OnItemClickListener {
       
private
GridView
gridView
;
       
private
List<Map<String, Object>>
data
;
       
private
int
[]
icon
={};
       
private
String[]
iconname
={};
       
private
SimpleAdapter
simpleAdapter
;
       
@Override
       
protected
void
onCreate(Bundle savedInstanceState) {
              
super
.onCreate(savedInstanceState);
              setContentView(R.layout.
activity_main
);
              
gridView
=(GridView) findViewById(R.id.
gridview
);
              
//准备数据源
              
//新建适配器(simpleAdapter)
              
//gridview 加载适合配器
              
//gridview 配置事件监听器(Onitemclicklistener )
              
data
=
new
ArrayList<Map<String, Object>>();
              getdata();
              
simpleAdapter
=
new
SimpleAdapter(
this
, getdata(), R.layout.
item
,
new
String[]{
"image"
,
"text"
},
new
int
[]{R.id.
image
,R.id.
text
});
              
gridView
.setAdapter(
simpleAdapter
);
              
//监听器每一列图标的点击事件。
              
gridView
.setOnItemClickListener(
this
);
 
       }
       
       
private
List<Map<String, Object>> getdata() {
              Map<String, Object> map=
new
HashMap<String,Object>();
              
for
(
int
i = 0; i <
icon
.
length
; i++) {
                     map.put(
"image"
,
icon
[i]);
                     map.put(
"text"
,
iconname
[i]);
              }
              
data
.add(map);
              
return
data
;
   
       }
       
@Override
       
public
void
onItemClick(AdapterView<?> parent, View view,
int
position,
long
id) {
              Toast. makeText(
this
, position, Toast.
LENGTH_LONG
).show();
              
       }
}

转载于:https://www.cnblogs.com/stareblankly/p/4829273.html

你可能感兴趣的文章
redis(一) 安装以及基本数据类型操作
查看>>
厚积薄发,拥抱 .NET 2016
查看>>
SQL2000 2005 破解函数,过程,触发器,视图
查看>>
美味辣子炒鸡
查看>>
开发可统计单词个数的Android驱动程序(2)
查看>>
一次向svn中增加所有新增文件 svn add all new files【转】
查看>>
Ubuntu 14.04配置LAMP(Linux、Apache、MySQL、PHP)
查看>>
字符串数组连接例子(原创)
查看>>
Linux设备驱动之Ioctl控制【转】
查看>>
Winform文件下载之断点续传
查看>>
TCP三次握手
查看>>
ABP理论学习之Web API控制器(新增)
查看>>
栈的应用之判断括号匹配
查看>>
让工具类不可实例化
查看>>
EntityFramework Core 1.1是如何创建DbContext实例的呢?
查看>>
iOS开发-NSPredicate
查看>>
MVC模式与struts框架
查看>>
Linux系统的中断、系统调用和调度概述【转】
查看>>
人月神话-人月:项目滞后的原因分析
查看>>
linux驱动学习(二) Makefile高级【转】
查看>>