android 获取当前layout,OnGlobalLayoutListener获得一个视图的高度

我们知道在oncreate中View.getWidth和View.getHeight无法获得一个view的高度和宽度,这是因为View组件布局要在onResume回调后完成。所以现在需要使用getViewTreeObserver().addOnGlobalLayoutListener()来获得宽度或者高度。这是获得一个view的宽度和高度的方法之一。

OnGlobalLayoutListener 是ViewTreeObserver的内部类,当一个视图树的布局发生改变时,可以被ViewTreeObserver监听到,这是一个注册监听视图树的观察者(observer),在视图树的全局事件改变时得到通知。ViewTreeObserver不能直接实例化,而是通过getViewTreeObserver()获得。

除了OnGlobalLayoutListener ,ViewTreeObserver还有如下内部类:

interfaceViewTreeObserver.OnGlobalFocusChangeListener

当在一个视图树中的焦点状态发生改变时,所要调用的回调函数的接口类

interfaceViewTreeObserver.OnGlobalLayoutListener

当在一个视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变时,所要调用的回调函数的接口类

interfaceViewTreeObserver.OnPreDrawListener

当一个视图树将要绘制时,所要调用的回调函数的接口类

interfaceViewTreeObserver.OnScrollChangedListener

当一个视图树中的一些组件发生滚动时,所要调用的回调函数的接口类

interfaceViewTreeObserver.OnTouchModeChangeListener

当一个视图树的触摸模式发生改变时,所要调用的回调函数的接口类

其中,我们可以利用OnGlobalLayoutListener来获得一个视图的真实高度。private int mHeaderViewHeight;

private View mHeaderView;

.....

mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(

new OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

mHeaderViewHeight = mHeaderView.getHeight();

mHeaderView.getViewTreeObserver()

.removeGlobalOnLayoutListener(this);

}

});

但是需要注意的是OnGlobalLayoutListener可能会被多次触发,因此在得到了高度之后,要将OnGlobalLayoutListener注销掉。另外mHeaderViewHeight和mHeaderView都需要写在当前java文件类(比如Activity)的成员变量中。不能直接在onCreate中定义否则会编译不通过:

Cannot refer to a non-final variable sHeight inside an inner class defined in a different method

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,作为AI语言模型,我无法生成实际的Android代码。但是,我可以给您提供一个登录布局的示例: ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/login_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <ImageView android:id="@+id/logo" android:layout_width="96dp" android:layout_height="96dp" android:layout_centerHorizontal="true" android:layout_marginBottom="32dp" android:src="@drawable/logo" /> <EditText android:id="@+id/email_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/logo" android:layout_marginTop="32dp" android:hint="Email" /> <EditText android:id="@+id/password_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/email_input" android:layout_marginTop="16dp" android:hint="Password" android:inputType="textPassword" /> <Button android:id="@+id/login_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/password_input" android:layout_marginTop="32dp" android:text="Login" /> <TextView android:id="@+id/forgot_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/login_button" android:layout_centerHorizontal="true" android:layout_marginTop="16dp" android:text="Forgot Password?" /> </RelativeLayout> ``` 该布局包括一个logo图像,一个输入电子邮件地址的EditText,一个输入密码的EditText,一个登录按钮和一个忘记密码的TextView。您可以根据自己的需求进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值