安卓控件高度获取(2)

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值