android获取根View的方法

https://blog.csdn.net/lloha/article/details/51496172
https://blog.csdn.net/c_cayujie/article/details/54381008
1、获取上级view的方法汇总

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

2、方法介绍

这里写图片描述

2.1、getRootView特例

布局(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}

结论:如果View在代码中通过View.inflate实例化,在没有添加到显示界面前,getRootView获得的是Xml的根布局。添加后getRootView获得的是MultiPhoneDecorView。

3、使用场景

用途:状态栏、标题栏、键盘的高度
注意:以下代码不能在onCreate里面使用,否则获取的高度为0,你可以放在onWindowFocusChanged等等

3.1、状态栏高度:

getDecorView的getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。 于是,我们就可以算出状态栏的高度了。

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

3.2、标题栏高度:

findViewById(android.R.id.content)是不包括标题栏的,我们可以通过它获取标题栏的高度。

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

3.3、键盘高度:

  • 1、键盘没打开时获取android.R.id.content的可见区域高度height1,
  • 2、键盘打开时再获取android.R.id.content的可见区域高度height2,
  • 3、键盘的高度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下载:http://download.csdn.net/detail/lloha/9547233

猜你喜欢

转载自blog.csdn.net/az44yao/article/details/112755018