安卓控件高度获取(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
原创粉丝点击