Intellij IDEA/Android Studioでマージルートタグを使用してレイアウトをプレビューする 質問する

Intellij IDEA/Android Studioでマージルートタグを使用してレイアウトをプレビューする 質問する

LinearLayout に基づいて複合コンポーネントを開発していると仮定します。そのため、次のようなクラスを作成します。

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 として機能し、次のような表示になります。マージしてプレビュー

(Android Studio ですが、Intellij IDEA も同様です。Eclipse についてはわかりません)

プレビューはレイアウトの開発を大幅に高速化しますが、一部のレイアウトでさえこのような大きな助けが失われるのは残念です。プレビューがmerge特定のレイアウトでタグをどのように解釈するかを指定する方法があるでしょうか?

ベストアンサー1

新しいparentTagツール属性(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

おすすめ記事