最近做项目自定义控件时候想获取控件中元素参数,但各种失败,网上搜了下,自己也总结一下。
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("+++++++++++++++++++我点了返回键,马上要跳转了++++++++++++++++++");
}
}