安卓控件高度获取(2)
来源:互联网 发布:搜索关键词优化 编辑:程序博客网 时间:2024/06/02 19:01
1、悬浮框的内容是通过网络数据动态绑定
////////AsyncTask异步获取网络数据:
private class WareTask extends AsyncTask<Void, Void, HashMap<String, Object>> { ProgressDialog dialog = null; @Override protected void onPreExecute() { if (dialog == null) { dialog = ProgressDialog.show(MymainWindActivity.this, "", "正在加载..."); dialog.show(); } } @Override protected HashMap<String, Object> doInBackground(Void... arg0) { String url = ""; url = "http://192.168.1.114:8080/yamadv/getAllSysparam"; // 请求数据,返回json String json = GetHttp.RequstGetHttp(url); //拼凑hasmap数据,并返回 HashMap<String,Object> map=null; JSONArray jsa = null; try { JSONObject jso=new JSONObject(json); jsa=jso.getJSONArray("rows"); int i=0; map=new HashMap<String, Object>(); for(i=0;i<jsa.length();i++){ JSONObject jsob=jsa.getJSONObject(i); map.put("name", jsob.get("paramName")); map.put("paramValue", jsob.get("paramValue")); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } //System.out.println("数据:"+json); return map; } @SuppressWarnings("unchecked") @Override protected void onPostExecute(HashMap<String, Object> result) { if (dialog != null && dialog.isShowing()) { dialog.dismiss(); dialog = null; } // 如果网络数据请求失败,那么显示默认的数据 if (result != null && result.get("name") != null) { // 得到data字段的数据 String name="dd"; String id="0"; try{ name=(String) result.get("name"); id=(String) result.get("paramValue"); }catch(Exception ex){} System.out.println("hashmap的数据:"+name); txt1.setText(name); txt2.setText(id); } else { } } }
通过上篇博客的四种方法均可获取控件的高度和宽度!
/////执行的顺序:
(1)、windowmanager中引入view
(2)、new WareTask().execute();
(3)、onresume方法:
@Override protected void onResume() { view.postDelayed(new Runnable() { @Override public void run() { View iv1 = view.findViewById(R.id.floatlayout);//(R.id.listview1); String msg1 = "iv1' width:" + iv1.getWidth() + " height:" + iv1.getHeight() + " measuredWidth:" + iv1.getMeasuredWidth() + "measuredHeight:" + iv1.getMeasuredHeight(); // iv1' width:480 height:2967 // measuredWidth:480measuredHeight:2967 System.out .println("@@@@@@@@@@@ onWindowFocusChanged() " + msg1); } }, 300); super.onResume(); }
(4)、在oncreate中执行的方法:
// 在oncreate中进行获取控件的高和宽----和onresume的方法获取的都是全局高度 view.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { view.postDelayed(new Runnable() { @Override public void run() { View iv1 = view.findViewById(R.id.floatlayout);//(R.id.listview1); // iv1' width:480 height:2967 // measuredWidth:480measuredHeight:2967 listview1 String msg1 = "iv1' width:" + iv1.getWidth() + " height:" + iv1.getHeight() + " measuredWidth:" + iv1.getMeasuredWidth() + "measuredHeight:" + iv1.getMeasuredHeight(); System.out.println("onWindowFocusChanged() " + msg1); } }, 300); } });
(5)、onWindowFocusChanged方法中获取:
2、总结:
在oncreate中执行,并且其他的方法先执行,并且只执行一次(快关控制):
ViewTreeObserver vto = floatlayout.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { if (hasMeasured == false) { int height = floatlayout.getMeasuredHeight(); int width = floatlayout.getMeasuredWidth(); // 获取到宽度和高度后,可用于计算 hasMeasured = true; } return true; } });
0 0
- 安卓控件高度获取(2)
- 安卓获取状态栏(Status Bar)高度
- 安卓获取屏幕宽度和高度
- android 获取控件高度
- 获取控件的高度
- 获取控件宽高度
- 获取控件的高度
- 安卓自定义控件之item高度不同的viewpager
- #安卓杂记(七):自定义控件及属性获取
- 安卓开发 获取手机状态栏的高度
- 安卓获取屏幕真实高度(除去虚拟键盘)
- 获取安卓控件的大小
- 安卓获取控件的宽高
- 安卓辅助功能获取控件id
- 获取安卓控件的大小
- 安卓控件2
- Android获取控件的高度
- 获取控件高度等信息
- 栈和队列
- JAVA中二叉树非递归算法前序,中序,层序遍历
- OpenLayers 3 之 Overlay(覆盖物)详解
- 关于第七课习题改进
- Java IO流读取大数据文件
- 安卓控件高度获取(2)
- Android-HttpClient连接网络获取数据
- HashSet 源代码
- 牛客网 | 按之字形顺序打印二叉树
- 杭电ACM3117——Crixalis's Equipment
- Linux<十二> 磁盘管理
- LEETCODE--ZigZag Conversion
- NSArray创建和遍历
- 第三章 回归线性模型