首页 文章 精选 留言 我的

精选列表

搜索[android things],共10000篇文章
优秀的个人博客,低调大师

Android -- MeasureSpec

自定义控件都会去重写View的onMeasure方法,因为该方法指定该控件在屏幕上的大小。 protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) onMeasure传入的两个参数是由上一层控件传入的大小,有多种情况,重写该方法时需要对计算控件的实际大小,然后调用setMeasuredDimension(int, int)设置实际大小。 onMeasure传入的widthMeasureSpec和heightMeasureSpec不是一般的尺寸数值,而是将模式和尺寸组合在一起的数值。 常用的三个函数 int getMode(int measureSpec)//根据提供的测量值(格式)提取模式(上述三个模式之一) int getSize(int measureSpec)//根据提供的测量值(格式)提取大小值(这个大小也就是我们通常所说的大小) int makeMeasureSpec(int size,int mode)//根据提供的大小值和模式创建一个测量值(格式) 得到模式: int mode = MeasureSpec.getMode(widthMeasureSpec) 得到尺寸: int size = MeasureSpec.getSize(widthMeasureSpec) Mode mode共有三种情况,取值分别为 MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY, MeasureSpec.AT_MOST MeasureSpec.EXACTLY是精确尺寸,当我们将控件的layout_width或layout_height指定为具体数值时如andorid:layout_width="50dip",或者为FILL_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。 MeasureSpec.AT_MOST是最大尺寸,当控件的layout_width或layout_height指定为WRAP_CONTENT时,控件大小一般随着控件的子空间或内容进行变化,此时控件尺寸只要不超过父控件允许的最大尺寸即可。因此,此时的mode是AT_MOST,size给出了父控件允许的最大尺寸。 MeasureSpec.UNSPECIFIED是未指定尺寸,这种情况不多,一般都是父控件是AdapterView,通过measure方法传入的模式。 Code 控件根据横竖屏、屏幕大小自适应: @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); if (0 == mRatioWidth || 0 == mRatioHeight) { setMeasuredDimension(width, height); } else { if (width < height * mRatioWidth / mRatioHeight) { setMeasuredDimension(width, width * mRatioHeight / mRatioWidth); } else { setMeasuredDimension(height * mRatioWidth / mRatioHeight, height); } } } public void fitWindow(int width, int height) { if (width < 0 || height < 0) { throw new IllegalArgumentException("Size cannot be negative."); } mRatioWidth = width;//屏幕宽 mRatioHeight = height;//屏幕高 requestLayout();//促使调用onMeasure } 我是天王盖地虎的分割线 本文转自我爱物联网博客园博客,原文链接:http://www.cnblogs.com/yydcdut/p/4170629.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android -- CursorAdapter

CursorAdapter CursorAdapter继承于BaseAdapter,它是个虚类,它为cursor和ListView提供了连接的桥梁。 public abstract class CursorAdapter extends BaseAdapter 注意cursor的必须要有个命名为"_id"的列。比如Contacts._ID就为"_id" 必须实现以下函数: abstract View newView(Context context, Cursor cursor, ViewGroup parent) abstract void bindView(View view, Context context, Cursor cursor) newView该函数第一次回调用后,如果数据增加后也会再调用,但是重绘是不会调用的。 数据增加后,回调用该函数来生成与新增数据相对应的view。 bindView函数第一次回调用后,如果数据更新也会再调用,但重绘会再次调用的。 总的来说应该是在调用bindView如果发现view为空会先调用newView来生成view。 Code public class MySqliteOpenhelper extends SQLiteOpenHelper { public MySqliteOpenhelper(Context context, int version) { super(context, "dianhuaben.db", null, version); } @Override public void onCreate(SQLiteDatabase db) {//注意:使用CursorAdapter时,创建表必须有以_id为列名的列 String sql = "CREATE TABLE dhb (_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } public void createCursorAdapter(Cursor cursor) { //游标适配器,构造方法,传入cursor mAdapter = new CursorAdapter(this, cursor) {//重写两个方法 @Override public View newView(Context context, Cursor cursor, ViewGroup parent) {//找到布局和控件 ViewHolder holder = new ViewHolder(); LayoutInflater inflater = getLayoutInflater(); View inflate = inflater.inflate(R.layout.listview_item, null); holder.item_tv_name = (TextView) inflate.findViewById(R.id.item_tv_name); holder.item_tv_phone = (TextView) inflate.findViewById(R.id.item_tv_phone); inflate.setTag(holder); return inflate;//返回的view传给bindView。 } @Override public void bindView(View view, Context context, Cursor cursor) { // 把数据设置到界面上 ViewHolder holder = (ViewHolder) view.getTag(); String name = cursor.getString(cursor.getColumnIndex("name")); String phone = cursor.getString(cursor.getColumnIndex("phone")); holder.item_tv_name.setText(name); holder.item_tv_phone.setText(phone); } }; }; SimpleCursorAdapter 简单提及一下 public void createSimpleCursorAdapter(Cursor cursor) {// SimpleCursorAdapter继承了CursorAdapter继承了BaseAdapter String[] from = {TABLE_NAME_NAME,TABLE_NAME_PHONE};//列名与控件id一一对应 int[] to = {R.id.item_tv_name,R.id.item_tv_phone}; //用的是SimpleCursorAdapter,用法和simpleAdapter相似 mAdapter = new SimpleCursorAdapter(MainActivity.this, R.layout.listview_item, cursor, from, to); } 使用SimpleCursorAdapter,时,创建表必须有以_id为列名的列 我是天王盖地虎的分割线 本文转自我爱物联网博客园博客,原文链接:http://www.cnblogs.com/yydcdut/p/4090723.html,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册