android 动态宽高,Android开发动态获取控件宽高

最近做项目自定义控件时候想获取控件中元素参数,但各种失败,网上搜了下,自己也总结一下。

package com.example.androidtest_02;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewTreeObserver;

import android.view.ViewTreeObserver.OnGlobalLayoutListener;

import android.view.WindowManager.LayoutParams;

import android.widget.Button;

import android.widget.TextView;

/**

*

* 这个activity测试 : 1.Activity的生命周期 2.程序动态获取控件的宽高

*

* 结论:activity启动时候:onCreate()-->onStart()--->onResum()-->onAttachedToWindow()-->

* onWindowFocusChanged()

*

* 当另外一个activity be in front of

* 的时候:onPause()--->onWindowFocusChanged()-->onStop()

*

* 当第一个activity恢复到最前面时候:onRestart()--->onStart()--->onResum()--->

* onWindowFocusChanged()

*

* 当activityfinish的时候:onPause()-->onWindowFocusChanged()-->onStop()-->onDestroy(

* )

*

*

* onAttachedToWindow()此时打印出了控件宽高,其他地方的打印为0 ,so,activity全面加载完了之后才会获取到控件的宽高,

* 当我们需要获取控件的宽高时,需要等activity加载完。

*

* 第二种与第三种方式:即使实在oncreate()方法里面声明了,回调里面的内容也只能在onResume()方法被调用后才能得到执行。

*/

public class MainActivity extends Activity implements OnClickListener {

public void printLog(String s) {

Log.e("-------------------------------", s);

}

Button btn1;

Button btn2;

TextView textview1;

/**

* 第一种获取宽高的方式。

*/

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

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

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn1 = (Button) findViewById(R.id.btn1);

btn1.setOnClickListener(this);

btn2 = (Button) findViewById(R.id.btn2);

textview1 = (TextView) findViewById(R.id.textview1);

textview1.measure(w, h);

// int height =textview1.getMeasuredHeight();

// int width =textview1.getMeasuredWidth();

printLog("直接获取宽高:width:" + textview1.getWidth() + "---->height:"

+ textview1.getHeight());

printLog("使用mesure()获取宽高:width:" + textview1.getWidth()

+ "---->height:" + textview1.getHeight());

// 即使实在oncreate()方法里面声明了,回调里面的内容也只能在onResume()方法被调用后才能得到执行。

// 第二种获取宽高的方式

ViewTreeObserver viewTreeObserver = textview1.getViewTreeObserver();

viewTreeObserver

.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

@Override

public boolean onPreDraw() {

printLog("使用ViewTreeObserver 获取宽高:width:"

+ textview1.getMeasuredWidth() + "---->height:"

+ textview1.getMeasuredHeight());

return true;

}

});

// 第三种获取宽高的方式

ViewTreeObserver vtb1 = textview1.getViewTreeObserver();

vtb1.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

textview1.getViewTreeObserver().addOnGlobalLayoutListener(this);

printLog("使用OnGlobalLayoutListener 获取宽高:width:"

+ textview1.getWidth() + "---->height:"

+ textview1.getHeight());

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.activity_main, menu);

return true;

}

@Override

protected void onStart() {

super.onStart();

printLog("onstart()");

printLog("width:" + textview1.getWidth() + "---->height:"

+ textview1.getHeight());

}

@Override

protected void onResume() {

super.onResume();

printLog("onResume()");

printLog("width:" + textview1.getWidth() + "---->height:"

+ textview1.getHeight());

}

@Override

public void onAttachedToWindow() {

super.onAttachedToWindow();

printLog("onAttachedToWindow()");

printLog("width:" + textview1.getWidth() + "---->height:"

+ textview1.getHeight());

}

@Override

public void onWindowFocusChanged(boolean hasFocus) {

super.onWindowFocusChanged(hasFocus);

printLog("onWindowFocusChanged()");

printLog("width:" + textview1.getWidth() + "---->height:"

+ textview1.getHeight());// 此时打印出了控件宽高,其他地方的打印为0

}

@Override

protected void onRestart() {

super.onRestart();

printLog("onRestart()");

}

@Override

public void onWindowAttributesChanged(LayoutParams params) {

super.onWindowAttributesChanged(params);

printLog("onWindowAttributesChanged()");

}

@Override

protected void onPause() {

super.onPause();

printLog("onPause()");

}

@Override

protected void onStop() {

super.onStop();

printLog("onStop()");

}

@Override

protected void onDestroy() {

super.onDestroy();

printLog("onDestroy()");

}

@Override

public void onClick(View v) {

if (v.equals(btn1)) {

printLog("+++++++++++++++++++马上要跳转了++++++++++++++++++");

Intent intent = new Intent(MainActivity.this, Activity1.class);

startActivity(intent);

} else if (v.equals(btn2)) {

printLog("+++++++++++++++++++马上finish了++++++++++++++++++");

this.finish();

}

}

@Override

public void onBackPressed() {

printLog("+++++++++++++++++++我点了返回键,马上要跳转了++++++++++++++++++");

}

}

0818b9ca8b590ca3270a3433284dd417.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>