上Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TextView        android:id="@+id/tv_hello"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />LinearLayout>

上Activity,几种方法都写下来了:

package com.cn.measure;import android.annotation.SuppressLint;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.ViewTreeObserver;import android.view.ViewTreeObserver.OnGlobalLayoutListener;import android.widget.TextView;import android.widget.Toast;@SuppressLint("NewApi")public class MainActivity extends Activity{    private TextView tv_hello;    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv_hello = (TextView)findViewById(R.id.tv_hello);        getSizeWithViewTreeObserver();        getSizeWithPost();        getSizeWithMeasureByHand();    }    /**     * 如果tv_hello的mode是match_parent不能用这种方法     * 因为view的measure过程中需要知道父容器的剩余空间大小,这个时候无法知道父容器剩余空间大小     * 其实这种方法不推荐使用,测量出来的值可能不正确     */    private void getSizeWithMeasureByHand()    {        tv_hello.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);        int height = tv_hello.getMeasuredHeight();        int width = tv_hello.getMeasuredWidth();        Toast.makeText(MainActivity.this,            "getSizeWithMeasureByHand excuted: width is " + width + " \n Height is " + height,            0).show();    }    private void getSizeWithPost()    {        tv_hello.post(new Runnable()        {            @Override            public void run()            {                int width = tv_hello.getMeasuredWidth();                int height = tv_hello.getMeasuredHeight();                Toast.makeText(MainActivity.this,                    "getSizeWithPost excuted: width is " + width + " \n Height is " + height,                    0).show();            }        });    }    private void getSizeWithViewTreeObserver()    {        ViewTreeObserver observer = tv_hello.getViewTreeObserver();        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener()        {            @Override            public void onGlobalLayout()            {                tv_hello.getViewTreeObserver().removeGlobalOnLayoutListener(this);                int width = tv_hello.getMeasuredWidth();                int height = tv_hello.getMeasuredHeight();                Toast.makeText(MainActivity.this,                    "getSizeWithViewTreeObserver excuted: width is " + width + " \n Height is " + height,                    0).show();            }        });    }    @Override    public void onWindowFocusChanged(boolean hasFocus)    {        super.onWindowFocusChanged(hasFocus);        int width = tv_hello.getMeasuredWidth();        int height = tv_hello.getMeasuredHeight();        Toast.makeText(MainActivity.this,            "onWindowFocusChanged excuted: width is " + width + " \n Height is " + height,            0).show();    }}

四种方法:

  private void getSizeWithMeasureByHand() //不推荐使用  private void getSizeWithViewTreeObserver() //推荐使用  private void getSizeWithPost() //推荐使用  public void onWindowFocusChanged(boolean hasFocus) //推荐使用

更多相关文章

  1. Android得到SD卡文件夹大小以及删除文件夹操作
  2. Android动态设置View的位置和大小
  3. Android(安卓)系统(84)---查看分区的大小
  4. android自定义progressbar图片大小自适应
  5. Android中当item数量超过一定大小RecyclerView高度固定
  6. java/android 统计文件夹大小及删除文件夹下所有文件和路径
  7. Android(安卓)NavigationView 中 menu item 字体大小设置
  8. Android(安卓)ViewGroup中有关测量的方法
  9. android 测绘流程

随机推荐

  1. webView自适应屏幕大小
  2. Android UI开发第二十三篇――分享书架UI
  3. android使用c语言编程
  4. Android 之 四大组件、六大布局、五大存
  5. 【原创】删除Android预装软件包,…
  6. Android Re-installation failed解决方法
  7. Android程序内存管理必读
  8. Android 版本 相关参数修改(SpreadTrum)
  9. Android数据持久化——五种方式
  10. android从不同的layout添加tab页内容_tab