上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动态设置View的位置和大小
  2. android 横竖屏切换 分辨率大小设置的模拟器
  3. Android得到SD卡文件夹大小以及删除文件夹操作
  4. Android 系统(84)---查看分区的大小
  5. android自定义progressbar图片大小自适应
  6. Android中当item数量超过一定大小RecyclerView高度固定
  7. java/android 统计文件夹大小及删除文件夹下所有文件和路径
  8. Android NavigationView 中 menu item 字体大小设置
  9. 面试例题4:绘制5行文本,每一行的字体大小逐渐增加

随机推荐

  1. Android事情记载
  2. android手机游戏之绘图
  3. 解决unity 打包在Android(安卓)8.0上启动
  4. Buttons in button bars should be borde
  5. android 入门学习笔记 正则匹配 电话号码
  6. Android数据库更新并保留原来数据的实现
  7. Android强制竖屏
  8. Android(安卓)Stduio 3.0进行JNI和NDK开
  9. Android开发_多线程详解(九)
  10. android中获取设备的型号和手机厂商