成都优化网站厂家,服装网络推广方案,网站后台编辑器下载,宿州市建设局网站前言
先简单复习一下Android View 的 绘制顺序#xff1a;
1、onMeasure#xff08;测量#xff09;#xff0c;先根据构造器传进来的LayoutParams#xff08;布局参数#xff09;#xff0c;测量view宽高。
2、onLayout#xff08;布局#xff09;#xff0c;再根…前言
先简单复习一下Android View 的 绘制顺序
1、onMeasure测量先根据构造器传进来的LayoutParams布局参数测量view宽高。
2、onLayout布局再根据测量出来的宽高参数进行布局。
3、onDraw绘制最后绘制出View。
ps案例中用到了dataBinding 1、使用LayoutParams改变View高度
效果getHeight 和 getMeasuredHeight 的值是一样的没有区别;
getWidth 和 getMeasuredWidth 也是同理。 bind.btn.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {LinearLayout.LayoutParams params (LinearLayout.LayoutParams) bind.textBox.getLayoutParams(); params.height 10;bind.textBox.setLayoutParams(params);Log.d(TAG, 更新后 getHeight bind.textBox.getHeight() --- getMeasuredHeight bind.textBox.getMeasuredHeight());}});2、使用layout改变View高度
效果getHeight 的值在变化而 getMeasuredHeight 的值没有变化还是初始值。
getWidth 和 getMeasuredWidth 也是同理。 bind.btn.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {bind.textBox.layout(bind.textBox.getLeft(),bind.textBox.getTop(),bind.textBox.getRight(),bind.textBox.getBottom() 10);Log.d(TAG,更新后 getHeight bind.textBox.getHeight() --- getMeasuredHeight bind.textBox.getMeasuredHeight());}}); 3、区别
通过以上方式可以看出使用layout可以改变View 宽或高 度但并不会更新View原始测量值即使使用requestLayout()也不行。
源码因为它是final类型不可重写。 4、两种获取宽高值的应用场景
1、View布局完成就是View执行完onLayout使用 getWidth / getHeight这是View源码。 /*** Return the width of your view.** return The width of your view, in pixels.*/ViewDebug.ExportedProperty(category layout)public final int getWidth() {return mRight - mLeft;}/*** Return the height of your view.** return The height of your view, in pixels.*/ViewDebug.ExportedProperty(category layout)public final int getHeight() {return mBottom - mTop;}/*** Right position of this view relative to its parent.** return The right edge of this view, in pixels.*/ViewDebug.CapturedViewPropertypublic final int getRight() {return mRight;}/*** Left position of this view relative to its parent.** return The left edge of this view, in pixels.*/ViewDebug.CapturedViewPropertypublic final int getLeft() {return mLeft;}/*** Bottom position of this view relative to its parent.** return The bottom of this view, in pixels.*/ViewDebug.CapturedViewPropertypublic final int getBottom() {return mBottom;}/*** Top position of this view relative to its parent.** return The top of this view, in pixels.*/ViewDebug.CapturedViewPropertypublic final int getTop() {return mTop;}
1.1、自定义View 运行结果 Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);Log.d(TAG, onMeasure --- getLeft getLeft()); // 0Log.d(TAG, onMeasure --- getTop getTop()); // 0Log.d(TAG, onMeasure --- getRight getRight()); // 0Log.d(TAG, onMeasure --- getBottom getBottom()); // 0Log.d(TAG, onMeasure --- getWidth getWidth()); // 0Log.d(TAG, onMeasure --- getHeight getHeight()); // 0}Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);Log.d(TAG, onMeasure --- getLeft getLeft()); // 503Log.d(TAG, onMeasure --- getTop getTop()); // 871Log.d(TAG, onMeasure --- getRight getRight()); // 577Log.d(TAG, onMeasure --- getBottom getBottom()); // 923Log.d(TAG, onMeasure --- getWidth getWidth()); // 74 577 - 503Log.d(TAG, onMeasure --- getHeight getHeight()); // 52 923 - 871} 2、反之View没有布局完使用 getMeasuredWidth / getMeasuredHeight比如在onCreate中使用可以获取 宽高值如果在此 使用 getWidth / getHeight 返回会是0因为此时还没有布局完成。
注意在布局未完成前使用 getMeasuredWidth / getMeasuredHeight要先主动通知系统测量才会有值如果布局已经完成那就直接用不需要这一步
通知系统测量方法measure(0,0)直接都写0就好系统会返回正确的值。
Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);bind AppActivityMainBinding.bind(getLayoutInflater().inflate(R.layout.app_activity_main, null));setContentView(bind.getRoot());// 主动通知系统去测量bind.textBox的高度// 注意在onCreate中此时的布局还未完成只有执行了这句代码bind.textBox.getMeasuredHeight() 才会有值bind.textBox.measure(0,0);bind.showHeight.setText(getHeight bind.textBox.getHeight() --- getMeasuredHeight bind.textBox.getMeasuredHeight()); } 5、使用layout改变View宽高会引发的问题
前言提到onLayout布局是根据测量出来的宽高参数进行布局的虽然在视觉上改变了宽高但测量的宽高值还是原始值没有改变
而在Android布局体系中父View负责刷新、布局显示子View当子View需要刷新时则是通知父View来完成就是循环遍历调用子View的 measure / layout / draw 方法。
由此得出当布局中某个子View布局发生改变这个父View就开始循环遍历调用子View的layout通过布尔值changed判断当前子View是否需要重新布局changed为true表示当前View的大小或位置改变了 。
这时就会发现 之前通过layout改变宽高的View会被还原。因为onLayout布局是根据测量出来的宽高参数进行布局的重要的话说三遍。 案例
我新加了一个TextView将值显示在屏幕上。 TextViewandroid:idid/show_heightandroid:layout_widthwrap_contentandroid:layout_heightwrap_content/ View没有发生改变不它改变了但给TextView赋值后TextView的宽高发生改变通知父View刷新父View开始循环遍历子View的layout方法导致 通过layout改变宽高的View又根据 原始测量值重新布局还原了由于执行的太快所以视觉上看不到View这个过程。
日志 20:29:39.933 D MTextView --- onLayout --- bottom1127 --- changedtrue 20:29:39.935 D 更新TextView触发MTextView的 onLayout方法 20:29:39.973 D MTextView --- onLayout --- bottom1117 --- changedtrue 6、案例文件
MTextView.java
public class MTextView extends androidx.appcompat.widget.AppCompatTextView {public MTextView(NonNull Context context) {super(context);}public MTextView(NonNull Context context, Nullable AttributeSet attrs) {super(context, attrs);}Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int heightPxValue MeasureSpec.getSize(heightMeasureSpec);Log.d(TAG, MTextView --- onMeasure --- heightPxValue heightPxValue);}Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);Log.d(TAG, MTextView --- onLayout --- bottom bottom --- changed changed);}app_activity_main.xml
?xml version1.0 encodingutf-8?
layout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsdata/dataLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:gravitycenterandroid:orientationverticaltools:context.ui.activity.AppMainActivityTextViewandroid:idid/show_heightandroid:layout_widthwrap_contentandroid:layout_heightwrap_content/Buttonandroid:idid/btnandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textupdateandroid:textAllCapsfalse /com.example.xxx.ui.view.MTextViewandroid:idid/text_boxstylestyle/Font_303133_15_boldandroid:layout_width200dpandroid:layout_height100dpandroid:backgroundcolor/color_66000000android:gravitycenterandroid:texthello world //LinearLayout/layout
AppMainActivity.java
public class AppMainActivity extends AppCompatActivity {private AppActivityMainBinding bind;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);bind AppActivityMainBinding.bind(getLayoutInflater().inflate(R.layout.app_activity_main, null));setContentView(bind.getRoot());// bind.btn.setOnClickListener(new View.OnClickListener() {// Override// public void onClick(View v) {// LinearLayout.LayoutParams params (LinearLayout.LayoutParams) bind.textBox.getLayoutParams(); // params.height 10;// bind.textBox.setLayoutParams(params);// Log.d(TAG, 更新后 getHeight bind.textBox.getHeight() --- getMeasuredHeight bind.textBox.getMeasuredHeight());// }// });// bind.btn.setOnClickListener(new View.OnClickListener() {// Override// public void onClick(View v) {// bind.textBox.layout(// bind.textBox.getLeft(),// bind.textBox.getTop(),// bind.textBox.getRight(),// bind.textBox.getBottom() 10// );// Log.d(TAG,更新后 getHeight bind.textBox.getHeight() --- getMeasuredHeight bind.textBox.getMeasuredHeight());// }// });bind.btn.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {bind.textBox.layout(bind.textBox.getLeft(),bind.textBox.getTop(),bind.textBox.getRight(),bind.textBox.getBottom() 10);bind.showHeight.setText(getHeight bind.textBox.getHeight() --- getMeasuredHeight bind.textBox.getMeasuredHeight());Log.d(TAG,更新TextView触发MTextView的 onLayout方法);}});}} 总结
在View没有布局完成前想要获取 宽高使用 getMeasuredWidth / getMeasuredHeight记得先通知系统测量 反之只要显示在屏幕上getWidth / getHeight 就能拿到值还是时时数据。
补充一下如果在xml中给View设置了visibilitygone注意是xmlgetWidth / getHeight 也拿不到值如果是 visibilityinvisible不受影响。
再如果 在xml中给View设置了visibilitygone在代码中设置成setVisibility(View.VISIBLE)第一次也拿不到值因为还没有layout完成之后就可以拿到了后面再给它设置成setVisibility(View.GONE)也不会受影响因为已经布局过了。代码在这我都试过了核心就是看View有没有onLayout完成。
Activity Overridepublic void onWindowFocusChanged(boolean hasFocus) {super.onWindowFocusChanged(hasFocus);Log.d(TAG, getHeight bind.btn.getHeight()); // 0new Handler().postDelayed(new Runnable() {Overridepublic void run() {Log.d(TAG, getHeight bind.btn.getHeight()); // 0bind.btn.setVisibility(View.VISIBLE); // 设置显示Log.d(TAG, getHeight bind.btn.getHeight()); // 0}}, 2000);new Handler().postDelayed(new Runnable() {Overridepublic void run() {Log.d(TAG, getHeight bind.btn.getHeight()); // 126bind.btn.setVisibility(View.GONE); // 设置消失Log.d(TAG, getHeight bind.btn.getHeight()); // 126}}, 5000);new Handler().postDelayed(new Runnable() {Overridepublic void run() {Log.d(TAG, getHeight bind.btn.getHeight()); // 126}}, 8000);}
Xml Buttonandroid:idid/btnandroid:visibilitygoneandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textupdateandroid:textAllCapsfalse /