測量View(一):創建View并測量

測量View(二):測量寬高及真實寬高 http://www.lxweimin.com/p/18540e62ae3a
測量View(三):獲得測量寬高及真實寬高 http://www.lxweimin.com/p/cbd758a5b5cf
先不分析原碼, 上例子

1. 在onCreate中測量新建的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();
    }
Paste_Image.png

結果后圖,都是0
里面加上內容呢?

    @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();

    }
Paste_Image.png

結果還都是0
手動設置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();

    }

Paste_Image.png

結果還都是0
換種設置寬高的方式

    @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();

    }
Paste_Image.png

結果都是0

2. 在onCreate中測量新建的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();

    }

Paste_Image.png

結果與View的一樣

3. 通過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>
Paste_Image.png

結果都是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>
Paste_Image.png

結果不出意外的也是0

新創建的View或者從布局中填充的布局, 獲得其寬高都為0

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容