美文网首页
测量View(一):创建View并测量

测量View(一):创建View并测量

作者: MinicupSimon | 来源:发表于2017-08-16 11:40 被阅读0次

测量View(二):测量宽高及真实宽高 http://www.jianshu.com/p/18540e62ae3a
测量View(三):获得测量宽高及真实宽高 http://www.jianshu.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

相关文章

网友评论

      本文标题:测量View(一):创建View并测量

      本文链接:https://www.haomeiwen.com/subject/nmtirxtx.html