gpt4 book ai didi

android - 运行时 DrawerLayout 必须使用 MeasureSpec.EXACTLY 异常进行测量

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:14 26 4
gpt4 key购买 nike

我正在关注 this来自的教程,我正面临一个奇怪的问题。

我的 DrawerLayout 中的内容并不重要,但如果它的 layout_heightlayout_width 设置为硬编码数字以外的任何值,它抛出此运行时异常。

我看到很多关于在图形布局工具中尝试呈现输出时抛出此错误的报告,但找不到任何运行时错误。

我有最新的支持库,刚刚更新到版本 21.0.2

如果可能,我想避免对我的高度和宽度进行硬编码。

文件的开头是这样的。这样,它就粉碎了。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

堆栈跟踪是

12-04 00:57:51.051: E/AndroidRuntime(26254): FATAL EXCEPTION: main
12-04 00:57:51.051: E/AndroidRuntime(26254): Process: com.lablabla.homedestroyer, PID: 26254
12-04 00:57:51.051: E/AndroidRuntime(26254): java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:814)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.View.measure(View.java:17430)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.View.measure(View.java:17430)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.View.measure(View.java:17430)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
12-04 00:57:51.051: E/AndroidRuntime(26254): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.View.measure(View.java:17430)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1141)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.Choreographer.doCallbacks(Choreographer.java:580)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.Choreographer.doFrame(Choreographer.java:550)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.os.Handler.handleCallback(Handler.java:739)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.os.Handler.dispatchMessage(Handler.java:95)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.os.Looper.loop(Looper.java:135)
12-04 00:57:51.051: E/AndroidRuntime(26254): at android.app.ActivityThread.main(ActivityThread.java:5221)
12-04 00:57:51.051: E/AndroidRuntime(26254): at java.lang.reflect.Method.invoke(Native Method)
12-04 00:57:51.051: E/AndroidRuntime(26254): at java.lang.reflect.Method.invoke(Method.java:372)
12-04 00:57:51.051: E/AndroidRuntime(26254): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-04 00:57:51.051: E/AndroidRuntime(26254): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

如果必须对其进行硬编码,使其“适合”各种屏幕尺寸的最佳方法是什么?

最佳答案

尝试覆盖 DrawerLayout 的 on measure 方法

public class CustomDrawerLayout extends DrawerLayout {

public CustomDrawerLayout(Context context) {
super(context);
}

public CustomDrawerLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public CustomDrawerLayout(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);
}

}

关于android - 运行时 DrawerLayout 必须使用 MeasureSpec.EXACTLY 异常进行测量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27283348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com