測(cè)量View(二):測(cè)量寬高及真實(shí)寬高 http://www.reibang.com/p/18540e62ae3a
測(cè)量View(三):獲得測(cè)量寬高及真實(shí)寬高 http://www.reibang.com/p/cbd758a5b5cf
先不分析原碼廊驼, 上例子
1. 在onCreate中測(cè)量新建的View
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = new TextView(this);
int width = textView.getWidth();
int height = textView.getHeight();
int measuredWidthAndState = textView.getMeasuredWidthAndState();
int measuredWidth = textView.getMeasuredWidth();
int measuredHeight = textView.getMeasuredHeight();
int measuredHeightAndState = textView.getMeasuredHeightAndState();
}
結(jié)果后圖恰力,都是0
里面加上內(nèi)容呢?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = new TextView(this);
textView.setText("Minicup");
int width = textView.getWidth();
int height = textView.getHeight();
int measuredWidthAndState = textView.getMeasuredWidthAndState();
int measuredWidth = textView.getMeasuredWidth();
int measuredHeight = textView.getMeasuredHeight();
int measuredHeightAndState = textView.getMeasuredHeightAndState();
}
結(jié)果還都是0
手動(dòng)設(shè)置View的寬高呢柿估?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = new TextView(this);
textView.setWidth(200);
textView.setHeight(200);
int width = textView.getWidth();
int height = textView.getHeight();
int measuredWidthAndState = textView.getMeasuredWidthAndState();
int measuredWidth = textView.getMeasuredWidth();
int measuredHeight = textView.getMeasuredHeight();
int measuredHeightAndState = textView.getMeasuredHeightAndState();
}
結(jié)果還都是0
換種設(shè)置寬高的方式
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 200);
textView.setLayoutParams(layoutParams);
int width = textView.getWidth();
int height = textView.getHeight();
int measuredWidthAndState = textView.getMeasuredWidthAndState();
int measuredWidth = textView.getMeasuredWidth();
int measuredHeight = textView.getMeasuredHeight();
int measuredHeightAndState = textView.getMeasuredHeightAndState();
}
結(jié)果都是0
2. 在onCreate中測(cè)量新建的ViewGroup
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 200);
linearLayout.setLayoutParams(layoutParams);
int width = linearLayout.getWidth();
int height = linearLayout.getHeight();
int measuredWidthAndState = linearLayout.getMeasuredWidthAndState();
int measuredWidth = linearLayout.getMeasuredWidth();
int measuredHeight = linearLayout.getMeasuredHeight();
int measuredHeightAndState = linearLayout.getMeasuredHeightAndState();
}
結(jié)果與View的一樣
3. 通過(guò)LayoutInflator從XML布局中獲取View
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = LayoutInflater.from(this).inflate(R.layout.layout, null);
int width = view.getWidth();
int height = view.getHeight();
int measuredWidthAndState = view.getMeasuredWidthAndState();
int measuredWidth = view.getMeasuredWidth();
int measuredHeight = view.getMeasuredHeight();
int measuredHeightAndState = view.getMeasuredHeightAndState();
}
R.layout.layout
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</View>
結(jié)果都是0
我們改一下布局的寬高為固定值
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="300dp">
</View>
結(jié)果不出意外的也是0
新創(chuàng)建的View或者從布局中填充的布局, 獲得其寬高都為0