93

LinearLayout을 기반으로 복합 컴포넌트를 개발하고 있다고 가정 해 보겠습니다. 그래서, 우리는 다음과 같이 클래스를 만들 : 우리가 somelayout.xml의 루트로 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); 
    } 
} 

, 우리는 여분의보기 수준이있을 것이다, 그래서 우리는 병합 사용 태그 :

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

(그것은, 인 IntelliJ의 IDEA, 그냥 같은 안드로이드 스튜디오입니다 이클립스에 대해 나도 몰라)

미리보기는 레이아웃을 많이 개발하는 속도를 높이며, 일부 레이아웃의 경우에도 큰 도움이됩니다. 미리보기에서 특정 레이아웃에서 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

다음 페이지에서 Google 개발자가 신고 한 티켓을 찾을 수 있습니다. https://code.google.com/p/android/issues/detail?id=61652 – Neige

-4

대신 부모도 가능한 사용 사용자 정의 클래스입니다 merge like

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

그런 다음이 레이아웃을 직접 팽창시키고 결과보기를 SomeView으로 전송하십시오. Android 스튜디오에서 직접 부모 클래스 인 SomeView을 확인하고 LinerLayout과 같은 미리보기를 처리합니다. SomeViewonFinishInflate() 메서드를 사용하여보기를 findViewById()으로 바인딩 할 수 있습니다. 이 솔루션의 장점은 모든 레이아웃 정의 또는 스타일 정의를 레이아웃 파일에 직접 저장할 수 있다는 것입니다. 코드에서 setOrientation()과 같은 메서드를 사용할 수 없습니다.

+1

미리보기를 시도하면 무한 재귀가 발생하고 스택 오버플로가 발생하여 전체 Android Studio가 중단됩니다. – mato

관련 문제