93

LinearLayoutに基づいて複合コンポーネントを開発しているとしましょう。我々はsomelayout.xmlのルートとしてLinearLayoutを使いますならば、我々は、余分なビューのレベルを持っていますIntellij IDEA/Androidスタジオのマージルートタグ付きプレビューレイアウト

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

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

ので、私たちは、タグをマージ使用します:だから、私たちはこのようにクラスを作成

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

しかし中 Preview with merge

(それはのIntelliJ IDEAは、私にはわからないのEclipseについて、ちょうど同じであるAndroidのメーカー、です)

0:IDEの[プレビュー]タブには、常にでframeLayoutとして働き、そして私たちはそのような何かを見ることができますマージ

プレビューではレイアウトの開発がスピードアップしていますが、レイアウトによってはこのような大きな助けを失います。プレビューが特定のレイアウトで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スタジオ0.5.8でサポートされているツール:showIn。これを使用すると、<マージ>レイアウトをプレビューすることができます。

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

ツールとレイアウト/ layout_merge.xml:showIn:と

<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

Googleの開発者から報告されたチケットにアクセスできます: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のようなプレビューを処理します。 SomeViewonFinishInflate()メソッドを使用して、ビューをfindViewById()でバインドすることができます。 このソリューションの利点は、すべてのレイアウト定義またはスタイル定義をレイアウトファイルに直接入れることができることです。コードにsetOrientation()のようなメソッドを使用することはできません。

+1

これは無限再帰を導入し、プレビューしようとするとスタックがオーバーフローし、Androidスタジオ全体が永久にハングアップします。 – mato

関連する問題