2013-05-17 129 views
30

導航抽屜的渲染誤差在ADT佈局編輯器

<FrameLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<ListView 
    android:id="@+id/left_drawer" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#111" 
    android:choiceMode="singleChoice" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="0dp" /> 

並稱到佈局XML作爲每Create a Navigation Drawer文檔產生

Exception raised during rendering: DrawerLayout must be measured with MeasureSpec.EXACTLY. 
Exception details are logged in Window > Show View > Error Log 

java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY. 
    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:591) 
    at android.view.View.measure(View.java:15518) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825) 
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
    at android.view.View.measure(View.java:15518) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825) 
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
    at android.view.View.measure(View.java:15518) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825) 
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
    at android.view.View.measure(View.java:15518) 
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.measureView(RenderSessionImpl.java:607) 
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:509) 
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:334) 
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325) 
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:440) 
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1545) 
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1302) 
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:1059) 
    at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate.delegatePageChange(LayoutEditorDelegate.java:686) 
    at com.android.ide.eclipse.adt.internal.editors.common.CommonXmlEditor.pageChange(CommonXmlEditor.java:360) 
    at org.eclipse.ui.part.MultiPageEditorPart$2.widgetSelected(MultiPageEditorPart.java:292) 
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248) 
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1276) 
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1300) 
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1285) 
    at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1079) 
    at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:3028) 
    at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1749) 
    at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:278) 
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1276) 
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3562) 
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3186) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1053) 
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942) 
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86) 
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588) 
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) 
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543) 
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149) 
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124) 
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196) 
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) 
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) 
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353) 
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629) 
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584) 
    at org.eclipse.equinox.launcher.Main.run(Main.java:1438) 
    at org.eclipse.equinox.launcher.Main.main(Main.java:1414) 
+0

您可以發佈完整的XML? – Karakuri

+0

也許這有助於某人。後來:我通過使DrawerLayout成爲根佈局來解決這個問題。如果任何佈局存在,你會得到這個異常。 –

回答

38

我有類似的問題。 如果您想要預覽佈局一會兒,您可以暫時將寬度和高度更改爲絕對值。

如: -

<android.support.v4.widget.DrawerLayout 

    xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="400px" 

    android:layout_height="800px"> 
+1

它適合我。 –

+2

增加:如果有包裹DrawerLayout那麼它應該有'機器人一個觀點:layout_height =「match_parent」'但不是wrap_parent否則會出現 – Diolor

+0

該錯誤終於..謝謝 –

2

定製視圖難以正確顯示的eclipse中的UI編輯器(同樣適用於Android Studio中的UI編輯器)。這個是正常的。

+2

我不認爲這是正常的,但確實是UI工具不適合Android。它在過去幾年有了很大的改進,但即使是像DrawerLayout這樣的官方組件也不能沒有錯誤地渲染。 –

9

Asmok的回答是不錯,但我這種類型的人總是忘記扭轉這種「臨時」的編輯。 (換句話說,臨時可能會變得非常大^^)

因此,我使用<include>標記並將佈局分成兩個文件。

<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"> 

    <!-- The main content view --> 
    <include layout="@layout/main_layout" /> 

    <!-- The navigation drawer --> 
    <include layout="@layout/navigation_drawer"/> 
</android.support.v4.widget.DrawerLayout> 

對於主要佈局(這是有問題的activit的UI),我現在可以使用Eclipse的圖形編輯器,因爲所有內容生活在一個單一的文件main_layout.xml
此外,我可以輕鬆地將導航抽屜包含在不同的活動中,而無需複製和粘貼代碼。

+0

它不適合我...... @yoshi。 –

+1

@HiteshDhamshaniya:聽到這可悲的消息。 :C也許寫另一個問題,你可以展示你的一些XML,以及你如何實現它,因爲閱讀起來會比通過評論討論這個問題要清楚得多。你可以使用'share'鏈接來引用這個答案,並在這裏的評論中提供你的問題的鏈接。所以它會讓別人更容易地跟隨你的路徑:)不管發生什麼事情:不要驚慌! – yoshi

+1

感謝您的回覆,但作爲臨時依據,我應該使用Asmok建議的方式,它的工作 –

16

從另一個Question我通過擴展DrawerLayout,並迫使正確Measure_Specs解決了這個問題:

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); 
    } 

} 
+1

儘管Google聲稱已在Android支持庫修訂版19(2013年10月)中修復了此問題,但這仍然是運行時問題的一個問題。這種黑客比我以前的黑客工作得更好。 – DataGraham

+1

它顯示較小的窗口:( – usman

19

這是固定在支持庫的18版,7月發佈。

在Eclipse中右鍵單擊您的項目>選擇android tools>添加支持庫。

固定。 :)

+1

在版本19.0.1中沒有修復,我目前正在使用 –

5

,我發現最簡單的辦法是把在預覽Theme.Holo.Light.Dialog.FixedSize功能和寧靜......

Image here

+0

+1來打開一個StackOverflow帳戶並提供一個解決方案,雖然它不是最優的 - 它確實有效。 - 用downvote槍在這裏快樂... – AVIDeveloper