RelativeLayout中的视图大小不正确
问题描述:
我在下面有一个RelativeLayout。它有一个可扩展的视图和一个显示框架布局。 RevealFrameLayout对齐屏幕底部,低于ExpandableView。当用户点击fab揭示动画时,将从fab中心开始并显示揭示帧布局。RelativeLayout中的视图大小不正确
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/expandable_frame"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:background="@color/colorPrimary"
android:elevation="16dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<TextView
android:id="@+id/app_name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|start"
android:padding="16dp"
android:text="@string/app_name"
android:textAppearance="@style/ExtraLargeText"/>
</FrameLayout>
<TextView
android:id="@+id/error_tv"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="@id/expandable_frame"
android:background="@color/mid_grey"
android:gravity="center"
android:textColor="@color/error_red"
android:visibility="invisible"
tools:text="@string/error_login"
tools:visibility="visible"/>
<android.support.design.widget.TextInputLayout
android:id="@+id/username_til"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/error_tv"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/username_tiet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username"
android:inputType="text"
android:lines="1"
android:nextFocusDown="@+id/password_tiet"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/password_til"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/username_til"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp">
<android.support.design.widget.TextInputEditText
android:id="@id/password_tiet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="textPassword"
android:lines="1"/>
</android.support.design.widget.TextInputLayout>
<FrameLayout
android:id="@+id/loading_reveal_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@id/expandable_frame"
android:background="@color/colorAccent"
android:visibility="invisible">
<com.wang.avi.AVLoadingIndicatorView
android:id="@+id/loading_animation_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="invisible"/>
</FrameLayout>
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="@id/expandable_frame"
android:visibility="gone"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/login_fab"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="16dp"
android:tint="@android:color/white"
app:srcCompat="@drawable/ic_ok"/>
</RelativeLayout>
我计算工厂的中心是这样的:
int cx = mLoginFab.getRight() - mLoginFab.getMeasuredWidth()/2;
int cy = mLoginFab.getBottom() - mLoginFab.getMeasuredHeight()/2;
但我得到的总是比晶圆厂的位置较低的坐标。它看起来像Reveal Frame Layout的底部超出了手机屏幕边界。
有没有人知道这个布局有什么问题,以及如何获得实际的晶圆中心坐标?
在此先感谢。
答
尝试getWidth()
和getHeight
而不是getMeasuredWidth
和getMeasuredHeight
。根据documentation for View
:
INT getMeasuredWidthAndState()该视图为计算由最近一次调用来测量(INT,INT)
返回全宽的测量信息。这个结果是由MEASURED_SIZE_MASK和MEASURED_STATE_TOO_SMALL定义的一个位掩码。 这只应在测量和布局计算中使用。使用getWidth()来查看布局后视图的宽度。
重点是我的,这适用于getMeasuredWidth
和getMeasuredHeight
为好。