1、GridView

1.GridView学习

GridView和ListView都是比较常用的多控件布局,而GridView更是实现九宫图的首选
main.xml:
<?xml version="1.0"  encoding="utf-8"?>
<GridView xmlns:....
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontanSpacing="10dp"
android:columWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
android:numColumns="auto_fit"
GridView的列数设置为自动,也可以指定具体数字,设置为自动之后系统会根据屏幕宽度、列宽来自动决定一行放置几行
 
android:columnWdith="90dp"
每列的宽度,也就是Item的宽度
 
android:stretchMode="columnWidth"
缩放与列宽大小同步
 
android:verticalSpacing="10dp"
两行之间的边距为10dp
 
android:horizontalSpacing="10dp"
两列之间的边距
 
android:listSelector="#00000000"
设置选中时item的背景颜色
 
android:scrollbars="none"
隐藏GridView的滚动条
 
android:fadeScrollbars="true"
设置为ture就可以实现滚动条的自动隐藏和显示。如果是ture则会自动隐藏与显示,若为false则一直会显示,默认是true
 
main_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingBottom="4dip" android:layout_width="fill_parent">
<ImageView
android:layout_height="wrap_content"
android:id="@+id/ItemImage"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_below="@+id/ItemImage"
android:layout_height="wrap_content"
android:text="TextView01"
android:layout_centerHorizontal="true"
android:id="@+id/ItemText">
</TextView>
</RelativeLayout>
 MainActivity.java
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridView=(GridView) findViewById(R.id.gridview); //生成动态数组,并且转入数据
ArrayList<HashMap<String,Object>> lsImageItem =new ArrayList<HashMap<String,Object>>();
for(int i=0;i<10;i++){
HashMap<String,Object> map=new HashMap<String,Object>();
map.put("ItemImage", R.drawable.icon);//添加图像资源的ID
map.put("ItemText","NO."+Stirng.valueOf(i);//按序号做ItemText
lsImageItem.add(map);
}
//生成适配器的ImageItem与动态数组一一对应
SimpleAdapter saImageItems new SimpleAdapter( this , lsImageItem , R.layout.main_item , new Strign[] {"ItemImage" ,"ItemText"}, new int[] {R.id.ItemImage ,R.id.ItemText}); gridVeiw.setAdapter(saImageItems);//添加并显示
//添加消息处理
gridView.setOnItemClickListener(new ItemClickListener);
} class ItemClickListener implements OnItemClickListener{
public void onItemClick(AdapterView<?> arg0,View arg1, int arg2, long arg3){
//在本例中arg2=arg3
HashMap<String, Object> item=(HashMap<String ,Object> arg0.getItemAtPosition(arg2);
//显示所选Item的ItemText
setTitle((String)item.get("ItemText"));
}
}

2、ListView

解决ScrollView中嵌套ListView,ListView只显示一行问题

在工作中,会遇到在ScrollView中嵌套ListView的需求,如果直接嵌套,可能会出现ListView只显示一行,但可以滑动的现象。

解决方法:

定义一个ListView基类,重写其中一个方法,然后再xml中引用

WListView.java

public class WListView extends ListView {
public WListView(Context context) {
super(context);
} public WListView(Context context, AttributeSet attrs) {
super(context, attrs);
} public WListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//根据模式据算每个child的高度和宽度
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}

xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"> <com.example.chm.list.WListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"> </com.example.chm.list.WListView>
</ScrollView>

这样就可以使用了

Android 高级UI组件(一)GridView与ListView的更多相关文章

  1. Android 高级UI组件(三)

    一.popupWindow 1.AlertDialog和PopupWindow最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManage ...

  2. Android 高级UI组件(二)

    1.ExpandableListView 显示垂直滚动两级列表的条目,只允许两个层次 整体思路: 要给ExpandableListView设置适配器,那么必须先设置数据源. 数据源,就是此处的适配器类 ...

  3. Android常见UI组件之ListView(二)——定制ListView

    Android常见UI组件之ListView(二)--定制ListView 这一篇接上篇.展示ListView中选择多个项及实现筛选功能~ 1.在位于res/values目录下的strings.xml ...

  4. Android 高级UI设计笔记07:RecyclerView 的详解

    1. 使用RecyclerView       在 Android 应用程序中列表是一个非常重要的控件,适用场合非常多,如新闻列表.应用列表.消息列表等等,但是从Android 一出生到现在并没有非常 ...

  5. 第30讲 UI组件之 GridView组件

    第30讲 UI组件之 GridView组件 1.网格布局组件GridView GridView是一个ViewGroup(布局控件),可使用表格的方式显示组件,可滚动的控件.一般用于显示多张图片,比如实 ...

  6. Android 高级UI设计笔记08:Android开发者常用的7款Android UI组件(转载)

    Android开发是目前最热门的移动开发技术之一,随着开发者的不断努力和Android社区的进步,Android开发技术已经日趋成熟,当然,在Android开源社区中也涌现了很多不错的开源UI项目,它 ...

  7. Android 高级UI设计笔记01:使用ExpandableListView组件(ListView的扩展)

    1.ExpandableListView是一个用来显示二级节点的ListView. 比如如下效果的界面: 2.使用ExpandableListView步骤 (1)要给ExpandableListVie ...

  8. Android用户界面 UI组件--AdapterView及其子类(一) ListView及各种Adapter详解

    ListView就是列表组件,一般通过继承ListActivity使用系统提供的ListView. 所有的AdapterView组件都需要有一个对应的Adapter作为适配器来显示列表中元素的布局方式 ...

  9. Android开发10.1:UI组件适配器AdapterView(创建ListView,Adapter接口)

    @version:Android4.3 API18 @author:liuxinming 概述               AdapterView继承了ViewGroup,它的本质是容器       ...

随机推荐

  1. js 基本用法和语法

    js 基础用法 点击事件     <!-- 第一种点击事件方式 -->   <!-- <div class="div" onclick="aler ...

  2. Spring一套全通4—持久层整合

    百知教育 - Spring系列课程 - 持久层整合 第一章.持久层整合 1.Spring框架为什么要与持久层技术进行整合 1. JavaEE开发需要持久层进行数据库的访问操作. 2. JDBC Hib ...

  3. MySQL:由于找不到VCRUNTIME140_1.dll,无法继续执行代码。重新安装程序可能会解决此问题

    我只是搬用工,记录一下 方法一: 安装这个微软常用运行库合集(https://www.repaik.com/), 链接:https://pan.baidu.com/s/1r4JJaUKjw-y1g3l ...

  4. CommonJS与ES6 Module的使用与区别

    CommonJS与ES6 Module的使用与区别 1. CommonJS 1.1 导出 1.2 导入 2. ES6 Module 2.1 导出 2.2 导入 3. CommonJS 与 ES6 Mo ...

  5. 寒武纪加速平台(MLU200系列) 摸鱼指南(二)--- 模型移植-环境搭建

    PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明   本文作为本人csdn blog的主站的备份.(Bl ...

  6. 普通邮箱设置客户端授权码并开启stmp服务以及关于QQ邮箱“命令顺序不正确。 服务器响应为:Error: need EHLO and AUTH first !”问题全指导

    Zoomla!逐浪CMS带有强大的邮局功能,可以用于发送邮件与进行事务管理. 其中邮局配置大家不太熟悉这里提供一系列教程. 1.首先在QQ邮箱当中开启"POP3/SMTP服务" 2 ...

  7. Linux基础一:基础命令

    Linux是什么,是干什么用的? 1.Linux是一个操作系统,电脑=软件+硬件,而操作系统就是特殊的软件 2.Linux系统内一切皆文件 3.bash shell 是红帽默认的shell(shell ...

  8. 架构小试之IDL

    本文转载自我自己的博客,感兴趣的老爷们可以关注~:https://www.miaoerduo.com/2021/11/16/arch-idl/ 为什么IDL的介绍也放在这里呢?一方面是我想不到放哪里, ...

  9. php多域名跳转nginx

    当web服务器nginx已经配置好了网站根目录时,需要增加另外的域名.但是由于限制必须在原来的网站根目录下面,nginx已经无法实现.只能通过php index页面进行调试.如下面: define(' ...

  10. OI省选算法汇总及学习计划(转)

    1.1 基本数据结构 数组(√) 链表(√),双向链表(√) 队列(√),单调队列(√),双端队列(√) 栈(√),单调栈(√) 1.2 中级数据结构 堆(√) 并查集与带权并查集(√) hash 表 ...