根据这个相关问题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)
答案 0 :(得分:3)
我决定通过扩展DrawerLayout本身来创建一个自定义的DrawerLayout,并且做了什么,但重复了DrawerLayout中的方法。但我唯一做的就是在 DrawerLayout 中@ 覆盖 onMeasure 方法。
下面是我的Custom DrawerLayout以及我在layout ::
中如何调用它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);
答案 1 :(得分:3)
由于新的ConstraintLayout,也可能发生这种情况。确保抽屉布局不在ConstraintLayout的层次结构中。
在Android上,这是一个非常愚蠢的错误。我会尝试为它提交一个错误。