博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android基础知识4-如何在初始化的时候得到控件的宽和高
阅读量:4207 次
发布时间:2019-05-26

本文共 3283 字,大约阅读时间需要 10 分钟。

onDraw的时候应该能获取到吧。不过我用的方法是:
获取到整个布局的View(可以在整个xml的顶级Layout上设置一个id然后在onCreate中findViewById出来),然后:
contentView.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {

    public boolean onPreDraw() {

        //这里坐标已经确定了
         return true;
    }
});

--------------------------------------------------------------------------------------------------------

Android 在OnCreate()中获取控件高度与宽度

本文为转载mzhjemail_新浪博客。原文地址:http://blog.sina.com.cn/s/blog_821e2bb101010yin.html

  试过在OnCreate()中获取控件高度与宽度的童鞋都知道,getWidth()与getHeight()方法返回是0,具体原因 看一下Activity的生命周期 就会明白。

  上代码:

方法一:

 

        int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);

        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        ssidtext.measure(w, h);
        int width =ssidtext.getMeasuredWidth();
        int height =ssidtext.getMeasuredHeight();
方法二:

 

        ViewTreeObserver vto = ssidtext.getViewTreeObserver();

        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                int height = ssidtext.getMeasuredHeight();
                int width = ssidtext.getMeasuredWidth();
                return true;
            }
        });
另外还搜索到一个方法:
-----------------------------------------------------------
先贴到这里。正在做试验。(试验完毕,这种方法得到的控件宽是正确的。

        ViewTreeObserver vto = mArrowUp.getViewTreeObserver();  

        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  

            @Override  

            public void onGlobalLayout() {  

               mArrowUp.getViewTreeObserver().removeGlobalOnLayoutListener(this);  

                Log.e("Width",Integer.toString(mArrowUp.getWidth()));  

            }  

        });  

 

具体用哪一种可以根据需求自己选择

 

--------------------------------------------------------------------------------------------------------

当我们在OnCreate里面初始化控件的时候,getWidth和getHeight的值是为0的

对于我们在main.xml里面定义的布局来说
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView  
    android:layout_width="50px" 
    android:layout_height="50px" 
    android:text="@string/hello"
    android:id="@+id/imageview"
    android:src="@drawable/photo1"
    />
    <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="nihao"
     android:id="@+id/haha"
    ></TextView>
</LinearLayout>

如果想得到ImageView的图片实际宽和高的方法

就是在OnStart方法中
Drawable drawable = imageView.getDrawable(); 
if(drawable!=null)(当没有设置xml的src属性或者没有在控件初始化的时候给ImageView装入图片的时候)
{
Log.i("Log",drawable.getIntrinsicWidth()+"--"+drawable.getIntrinsicHeight());
}
如果想得到在xml里面设置的布局宽高的话 可以通过
Log.i("Log",imageView.getLayoutParams().width+"--"+imageView.getLayoutParams().height);
但是这里要注意的是 xml布局里面必须设置的宽和高的单位是 px
这里打印出来的数值才和xml设置的一致
如果xml里面设置的是dip或者dp单位的画  在代码中还要转化 才可以得到正确的数值 
这里给定dip和px数值之间互相转化的公式或者说是方法
public static int dip2px(Context context , float dpValue)
{
      final float scale = context.getResources().getDisplayMetrics().density;
        
      return (int)(dpValue*scale+0.5f);
}
public static int px2dp(Context context , float pxValue)
{

      final float scale = context.getResources().getDisplayMetrics().density;
        
      return (int)(pxValue/scale+0.5f);

}
如果对于任意控件要估计测量它的高度的话
就要用到measure(widthMeasureSpec, heightMeasureSpec);
里面的参数 是用MeasureSpec的常量参数
UNSPECIFIED  不需要精确的测量出来的值 值为0
EXACTLY 准确的值
AT_MOST 最大估计值
调用控件的方法  getMeasuredWidth()和getMeasuredHeight()
--------------------------------------------------------------------------------------------------------
你可能感兴趣的文章
论文浅尝 | 通过共享表示和结构化预测进行事件和事件时序关系的联合抽取
查看>>
论文浅尝 | 融合多粒度信息和外部语言知识的中文关系抽取
查看>>
论文浅尝 | GMNN: Graph Markov Neural Networks
查看>>
廖雪峰Python教程 学习笔记3 hello.py
查看>>
从内核看epoll的实现(基于5.9.9)
查看>>
python与正则表达式
查看>>
安装.Net Framework 4.7.2时出现“不受信任提供程序信任的根证书中终止”的解决方法
查看>>
input type=“button“与input type=“submit“的区别
查看>>
解决Github代码下载慢问题!
查看>>
1.idea中Maven创建项目及2.对idea中生命周期的理解3.pom文件夹下groupId、artifactId含义
查看>>
LeetCode-栈|双指针-42. 接雨水
查看>>
stdin,stdout,stderr详解
查看>>
Linux文件和设备编程
查看>>
文件描述符
查看>>
终端驱动程序:几个简单例子
查看>>
登录linux密码验证很慢的解决办法
查看>>
fcntl函数总结
查看>>
HTML条件注释
查看>>
Putty远程服务器的SSH经验
查看>>
内核态与用户态
查看>>