93

假設我們正在開發基於LinearLayout的複合組件。所以,我們創建類是這樣的:在Intellij IDEA/Android Studio中預覽具有合併根標籤的佈局

public class SomeView extends LinearLayout { 
    public SomeView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     setOrientation(LinearLayout.VERTICAL); 
     View.inflate(context, R.layout.somelayout, this); 
    } 
} 

如果我們將使用LinearLayout作爲somelayout.xml根,我們將有額外的視圖的水平,所以我們使用合併標籤:

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Some text" 
     android:textSize="20sp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Some other text"/> 
</merge> 

但在在IDE合併預覽標籤總是作爲FrameLayout裏,我們會看到類似的東西: Preview with merge

(這是Android的工作室,IntelliJ IDEA的是一樣的,關於Eclipse我不知道)

預覽加速開發佈局很多,即使對於某些佈局,也會失去這麼大的幫助。可能有一種方法可以指定,Preview應該如何解釋merge特定佈局中的標籤?

+0

我想看到這種支持添加。 –

+0

這個工具屬性可能在未來某個時候可以解決。 https://code.google.com/p/android/issues/detail?id = 61652 – Jonas

回答

183

您可以使用新的parentTag工具屬性(added in Android Studio 2.2)指定合併標記的佈局類型,這將使​​佈局在佈局編輯器預覽中正確呈現。

因此,使用你的例子:

<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:parentTag="LinearLayout" 
    tools:orientation="horizontal"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Some text" 
     android:textSize="20sp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Some other text"/> 
</merge> 

注意:兩個android:layout_widthandroid:layout_height必須按順序指定的佈局,以在編輯器中正確顯示。

+2

完美!謝謝:) – friederbluemle

+2

這應該是正確的答案 –

+0

這就像一個魅力。謝謝。 +1 – Kashmir

61

編輯:過時的答案。請參閱starkej2的答案。


Android Studio 0.5.8增加了對工具的支持:showIn。通過使用它,可以預覽<合併>佈局。

http://tools.android.com/recent/androidstudio058released

佈局/ layout_merge.xml的工具:舒:

<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:custom="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:showIn="@layout/simple_relativelayout"> 

...... 

</merge> 

佈局/ simple_relativelayout.xml與包括:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include layout="@layout/layout_merge"/> 

</RelativeLayout> 
+13

好消息!對於複合組件不太方便,因爲我們只需要爲預覽添加額外的佈局。但總比沒有好。 – darja

+0

在Eclipse中支持類似的東西的任何想法? – Toguard

+3

您可以按照以下網址的谷歌開發者報告的票:https://code.google.com/p/android/issues/detail?id=61652 – Neige

-4

也可以使用自定義類用作代替父像

<com.mycompany.SomeView xmlns:android="http://schemas.android.com/apk/res/android"> 
... 
</com.mycompany.SomeView> 

然後直接膨脹此佈局並將結果視圖轉換爲SomeView。 Android工作室將直接檢查父級SomeView,並處理預覽如LinerLayout。 您可以使用onFinishInflate()方法在SomeView通過findViewById()結合的觀點。 該解決方案的好處是您可以將所有佈局定義或樣式定義直接放到佈局文件中,但不能在代碼中使用像setOrientation()這樣的方法。

+1

這引入了無限遞歸,並且在嘗試預覽時堆棧溢出,從而使整個Android Studio永久掛起。 – mato

相關問題