2012-05-24 138 views
11

在我的應用程序中,我經常依賴自定義構建視圖,如下例所示。Eclipse可視化編輯器中的自定義Android視圖

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:background="@color/light_grey" 
android:layout_height="match_parent" 
android:layout_width="fill_parent" > 

<TextView 
style="@style/CardTitle" 
android:id="@+id/card_title" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent"  
/> 

<com.whiterabbit.cards.ui.AspectRatioImageView 
    android:id="@+id/card_picture" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:adjustViewBounds="true" 
    android:layout_marginLeft="30dip" 
    android:layout_marginRight="30dip"  
    android:src="@drawable/boss" 
    /> 



<ListView 
    android:id="@+id/card_properties" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 

/> 

的問題是,我不知道如何,如果我之前在真實設備上或在模擬器上運行它,它會正確顯示。此外,如果我發現有問題,我將不得不對其執行更改並再次部署該應用程序,以查看這些更改是否按預期工作。

這可能是一個漫長而無聊的過程,尤其是如果應用程序需要一些交互才能訪問您要檢查的活動。

使用可視編輯器不起作用,因爲它無法加載自定義視圖。

是否有另一種方法來檢查視圖如何顯示,而無需在整個應用程序中運行?

+0

感謝@Blundell修訂我的問題 – fedepaol

回答

54

您可以在自定義視圖做到這一點:

if(!isInEditMode()){ 
    // Your custom code that is not letting the Visual Editor draw properly 
    // i.e. thread spawning or other things in the constructor 
} 

http://developer.android.com/reference/android/view/View.html#isInEditMode()

這允許您隱藏ADT插件XML Viewer代碼,希望顯示您的佈局!

View.isInEditMode()

指示此查看當前是否處於編輯模式。在開發人員工具中顯示時,通常處於編輯模式的視圖是 。對於 實例,如果此視圖正在由可視化用戶界面 構建器繪製,則此方法應返回true。子類應檢查此方法的返回值,以提供不同的行爲,前提是其正常行爲可能會干擾主機環境。對於 實例:該類在其構造函數中產生一個線程,圖形 代碼依賴於特定於設備的功能等。此方法通常在定製窗口小部件的繪圖代碼中檢查 。

0

您可以創建一個骨架活動,只加載您想要查看的視圖並使用足夠的數據填充該數據以使其顯示。

0

我正在使用Android Studio,因此我不確定此答案是否適用於您的情況。

我認爲你可以重寫的onDraw方法中的自定義視圖,這樣爲例保持內部圖像的長寬比:

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    // TODO: consider storing these as member variables to reduce 
    // allocations per draw cycle. 
    int paddingLeft = getPaddingLeft(); 
    int paddingTop = getPaddingTop(); 
    int paddingRight = getPaddingRight(); 
    int paddingBottom = getPaddingBottom(); 

    int w = getWidth() - paddingLeft - paddingRight; 
    int h = getHeight() - paddingTop - paddingBottom; 

    w = w<h ? w : h; 
    h = w; 

    // Draw the example drawable on top of the text. 
    if (dieDrawable != null) { 
     dieDrawable.setBounds(paddingLeft, paddingTop, 
     paddingLeft + w, paddingTop + h); 
     dieDrawable.draw(canvas); 
    } 
} 

這種方法無論是在模擬器和設計師運行。

它運行以及對於重繪視圖的任何事件(onSizeChanged,onLayout,等...)