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. SQL*Loader-704: Internal error: ulconnect: OCIServerAttach [0] ORA-12541: TNS:no listener

    使用/app/oracle/product/11.2.0/bin/sqlldr导入数据报错: 监听没有开启?检查发现监正常 猜测是监听端口不是默认的1521有关系,直接在sid里面加上数据库服务器的i ...

  2. CefSharp-基于C#的客户端开发框架技术栈开发全记录

    CefSharp简介 源于Google官方 CefSharp用途 CefSharp开发示例 CefSharp应用--弹窗与右键 不弹出子窗体 禁用右键 CefSharp应用--High DPI问题 缩 ...

  3. 菜鸡的Java笔记 api 文档

    package 包的用法    为什么需要 package ?        为了解决类之间的重名问题        为了便于管理类:合适类位于合适的包        package 怎么用?     ...

  4. HMS Core Insights第八期直播预告--创新能力解读

    [导读] 在上个月举办的HDC2021华为开发者大会上,全新登场的HMS Core 6向大家展示了包括媒体.图形.连接与通信等领域的众多全新开放能力.如仅用一部RGB摄像头的手机即可完成的3D建模,在 ...

  5. Duboo整合SpringBoot超级详细例子(附源码)

    dubbo3.0整合SpringBoot例子 dubbo新版本(3.0以上)在相对于 dubbo 旧版本(2.5.2.6.2.7),有很多的不相同的地方. 官方文档也说了新版本的特性: https:/ ...

  6. [cf461E]Appleman and a Game

    考虑我的每一次添加操作,要满足:1.该串是t的子串:2.该串不能与下一次的串开头字母构成t的子串.那么,设f[i][j][k]表示拼i次,第i次填入的开头字母是j,第i+1填入的开头字母是k的最短长度 ...

  7. [loj3313]序列

    定义$C_{i}$表示令$i,i+1,i+2,...$的位置减1的操作,定义$I_{i}$表示令$i,i+2,i+4,...$的位置减1的操作 结论1:一定存在一种最优解使得$\forall i$不同 ...

  8. C/C++ Qt Dialog 对话框组件应用

    在Qt中对话框分为两种形式,一种是标准对话框,另一种则是自定义对话框,在一般开发过程中标准对话框使用是最多的了,标准对话框一般包括 QMessageBox,QInputDialog,QFileDial ...

  9. Go语言核心36讲(Go语言实战与应用十二)--学习笔记

    34 | 并发安全字典sync.Map (上) 我们今天再来讲一个并发安全的高级数据结构:sync.Map.众所周知,Go 语言自带的字典类型map并不是并发安全的. 前导知识:并发安全字典诞生史 换 ...

  10. Linux设置默认的声卡

    首先查看自己电脑上的声卡 使用命令行查看 orangepi@orangepi3:~$ ll /proc/asound/ total 0 dr-xr-xr-x 4 root root 0 Dec 23 ...