android method to get root view

https://blog.csdn.net/lloha/article/details/51496172
https://blog.csdn.net/c_cayujie/article/details/54381008
1. Summary of methods for obtaining superior view

getParent(获取上一级View)
getRootView
getWindow().getDecorView()
findViewById(android.R.id.content)
((ViewGroup)findViewById(android.R.id.content)).getChildAt(0)

2. Method introduction

Write picture description here

2.1, getRootView special case

布局(R.layout.list_empty):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="15dp"
        android:text="暂无数据" />
</RelativeLayout>
代码:
    final View view = View.inflate(this, R.layout.list_empty, null);
    final TextView name = (TextView) view.findViewById(R.id.name);
    System.out.println("before View.inflate name getParent " + name.getParent());
    System.out.println("before View.inflate name getRootView " + name.getRootView());
    addContentView(view, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
    System.out.println("after View.inflate name getParent " + name.getParent());
    System.out.println("after View.inflate name getRootView " + name.getRootView());
结果:
before View.inflate name getParent android.widget.RelativeLayout{3d679e31 V.E..... ......ID 0,0-0,0}
before View.inflate name getRootView android.widget.RelativeLayout{3d679e31 V.E..... ......ID 0,0-0,0}
after View.inflate name getParent android.widget.RelativeLayout{3d679e31 V.E..... ......ID 0,0-0,0}
after View.inflate name getRootView com.android.internal.policy.impl.MultiPhoneWindow$MultiPhoneDecorView{c535bb V.E..... R.....ID 0,0-0,0}

Conclusion: If View is instantiated through View.inflate in the code, getRootView gets the root layout of Xml before it is added to the display interface. After adding, getRootView gets MultiPhoneDecorView.

3. Usage scenarios

Purpose: The height of the status bar, title bar, and keyboard.
Note: The following code cannot be used in onCreate, otherwise the obtained height is 0, you can put it in onWindowFocusChanged, etc.

3.1. The height of the status bar:

The getWindowVisibleDisplayFrame method of getDecorView can obtain the area displayed by the program, including the title bar, but not the status bar. So, we can calculate the height of the status bar.

Rect frame = new Rect();  
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
int statusBarHeight = frame.top;  

3.2. Title bar height:

findViewById(android.R.id.content) does not include the title bar, we can get the height of the title bar through it.

int contentTop = findViewById(android.R.id.content).getTop();  
//statusBarHeight是上面所求的状态栏的高度  
int titleBarHeight = contentTop - statusBarHeight;

3.3. Keyboard height:

  • 1. Get the visible area height height1 of android.R.id.content when the keyboard is not opened,
  • 2. Get the height2 of the visible area of ​​android.R.id.content when the keyboard is opened,
  • 3. The height of the keyboard height1-height2
    private View globalView;
    private int firstHeight;
    private boolean isFirst = true;

    globalView = findViewById(android.R.id.content);
    globalView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect rect = new Rect();
            globalView.getWindowVisibleDisplayFrame(rect);
            if (isFirst) {
                isFirst = false;
                firstHeight = rect.height();
            } else {
                int height = rect.height();
                if (height < firstHeight) {
                    System.out.println("键盘打开 " + (firstHeight - height));
                } else {
                    System.out.println("键盘关闭 ");
                }
            }
        }
    });

Demo download: http://download.csdn.net/detail/lloha/9547233

Guess you like

Origin blog.csdn.net/az44yao/article/details/112755018