FragerLayout在片段内提供Measure.EXACTLY错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FragerLayout在片段内提供Measure.EXACTLY错误相关的知识,希望对你有一定的参考价值。
根据这个相关的问题Make drawerlayout inside fragment full screen height我不确定是对还是错,但似乎片段总是不是全屏,即它只复制它的内容的布局属性并使用它,这就是为什么你可以有两个或多个片段的原因一个布局。
现在我有问题将Drawerlayout
的高度设置为match_parent
,我得到的错误的完整堆栈粘贴在下面。我已经尝试了所有我知道的方法来强制设置布局参数,但它只是不起作用,如果我给布局一个特定的dp,如500dp
那么它将起作用。
FATAL EXCEPTION: main
Process: whalescorp.fabuloxity, PID: 16319
java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1041)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
at android.view.View.measure(View.java:19857)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
这也可能因为新的ConstraintLayout而发生。确保抽屉布局不在ConstraintLayout的层次结构中。
这是Android上一个非常愚蠢的错误。我会尝试为它提交一个错误。
我决定通过扩展DrawerLayout本身来创建一个自定义的DrawerLayout,并且没有做任何事情,只是复制了DrawerLayout中的方法。但我唯一做的就是@Override DrawerLayout中的onMeasure方法。
下面是我的Custom DrawerLayout以及我在布局中如何调用它::
public class WhalesDrawerLayout extends DrawerLayout {
public WhalesDrawerLayout(Context context) {
super(context);
}
public WhalesDrawerLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public WhalesDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
在你的布局应该像这样调用:
<whalescorp.projectname.packagename.WhalesDrawerLayout
android:id="@+id/drawer"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:layout_height="match_parent"
android:layout_width="match_parent">
</whalescorp.projectname.packagename.WhalesDrawerLayout>
通过在activity.xml中键入类名,它将带来建议,你也可以轻松地获得它,同时你需要在它工作之前重建你的项目,你可以在你的代码中调用它::
final WhalesDrawerLayout drawerLayout = l.findViewById(R.id.drawer);
以上是关于FragerLayout在片段内提供Measure.EXACTLY错误的主要内容,如果未能解决你的问题,请参考以下文章