2012-06-25 3 views
18

Androidでより複雑なカスタムビューを構築する必要があります。最終的なレイアウトは次のようになります。しかしAndroid:既存のレイアウトを持つカスタムビューの複数のビューの子供

<RelativeLayout> 
    <SomeView /> 
    <SomeOtherView /> 
    <!-- maybe more layout stuff here later --> 
    <LinearLayout> 
    <!-- the children --> 
    </LinearLayout> 
</RelativeLayout> 

、私はちょうどこの定義でくださいしたいXMLファイルで(SomeViewを定義することなく、SomeOtherViewなど):

<MyCustomView> 
    <!-- the children --> 
</MyCustomView> 

は、Androidで、このことが可能であり、はいの場合:それを行う最もクリーンな方法は何でしょうか? 「addView()メソッドをオーバーライドする」と「すべてのビューを削除して後で追加する」という解決策がありましたが、どちらの方法を使用するかわかりません...

ありがとうあなたの助けに! :)

答えて

34

カスタムコンテナビューを作成することは絶対に可能であり、推奨されています。これは、Androidが複合コントロールと呼ぶものです。だから、:

public class MyCustomView extends RelativeLayout { 
    private LinearLayout mContentView; 

    public MyCustomView(Context context) { 
     this(context, null); 
    } 

    public MyCustomView(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public MyCustomView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     //Inflate and attach your child XML 
     LayoutInflater.from(context).inflate(R.layout.custom_layout, this); 
     //Get a reference to the layout where you want children to be placed 
     mContentView = (LinearLayout) findViewById(R.id.content); 

     //Do any more custom init you would like to access children and do setup 
    } 

    @Override 
    public void addView(View child, int index, ViewGroup.LayoutParams params) { 
     if(mContentView == null){ 
      super.addView(child, index, params); 
     } else { 
      //Forward these calls to the content view 
      mContentView.addView(child, index, params); 
     } 
    } 
} 

あなたが必要ですが、最終的に彼らはすべて私がサンプルに配置されたバージョンにコールバックを感じるようあなたがaddView()のように多くのバージョンを上書きすることができます。このメソッドをオーバーライドすると、フレームワークはXMLタグ内のすべての子を特定の子コンテナに渡します。その後、

、そのようにXMLを変更します。

のres /レイアウト/ custom_layout.xml

<merge> 
    <SomeView /> 
    <SomeOtherView /> 
    <!-- maybe more layout stuff here later --> 
    <LinearLayout 
     android:id="@+id/content" /> 
</merge> 

<merge>を使用する理由は、階層構造を簡素化することです。すべての子ビューは、カスタムクラス(RelativeLayout)にアタッチされます。 <merge>を使用しないと、すべての子に添付されたRelativeLayoutが別のRelativeLayoutに添付されてしまい、問題が発生する可能性があります。私は(ないカスタムビューで)他の場所で子どもたちを定義したいと言ったので

あなたが使用できるHTH

+0

ないソリューションが含まれます。より正確には、私はレイアウトファイルa.xml、b.xml、c.xmlを持っています。これらはすべて、同じカスタムビューを使用していますが、LinearLayoutの異なる子を使用しています。 – mreichelt

+0

これも完全に受け入れられます。私が説明したように、最も簡単な方法は 'addView()'をオーバーライドすることです。私は上記の例を編集しました。 – Devunwired

+0

ありがとうございました!このような長い時間が経過してもこれらの問題が解決されることを知りました! :) – mreichelt

-3

は、タグ

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> 
    <include android:id="@+id/nav_bar_layout" layout="@layout/nav_bar" android:layout_above="@+id/web_view" /> 
    <WebView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/web_view" android:layout_centerInParent="true" /> 
    <include android:id="@+id/admob_layout" layout="@layout/admob_layout" android:layout_below="@+id/web_view" /> 
</RelativeLayout> 
+1

私はすでに、カスタムビューを使用するたびにビューを定義(または含める)したくないと書いています。なぜなら、最終的にカスタムビューに依存するからです。 – mreichelt

関連する問題