8

私はbottom.xmlと呼ばれるレイアウトを持っている場合は、私の場合、レイアウトを別のレイアウトに埋め込む方法は?

bottom.xml:(単にのTextViewと編集テキストビューが含まれている)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:orientation="vertical" 
    > 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:gravity="center_horizontal" 
      android:text="@string/username" 

     /> 

     <EditText 
      android:id="@+id/name" 

      android:layout_width="120dip" 
      android:layout_height="50dip" 
      android:layout_gravity="center_horizontal"  
     /> 
</LinearLayout> 

はにどのような方法があります埋め込み上記bottom.xmlレイアウトは繰り返し書き込まれずいくつかのレイアウトファイルに同じコードを挿入します(他のレイアウトには、bottom.xmlと同じレイアウトを含む部分があります)。

例えば、私のはadmin.xmlレイアウトもちょうど内部bottom.xmlを埋め込む方法bottom.xml、と全く同じに見えるレイアウトの一部が含まれている場合admin.xml同じコードをもう一度書き込むのではなく、

admin.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
    > 
    ... 
    ... 
     <!--How to embed bottom.xml here--> 
    ... 
</LinearLayout> 

アンドロイドでそれを行うための方法がない場合は、どのような問題を回避する可能性が?

----------更新-----------

@xevincentが提案したように、私が使用してbottom.xmlを再利用することができます<include>タグ

しかし、どのようにresuedレイアウト内の要素のIDを変更するには?例えば

は、bottom.xml insdie、私はIDを変更する方法を、私は他のレイアウトでbottom.xmlレイアウトを再利用する際<editText android:id="@+id/other_name"><editText android:id="@+id/name">のIDを変更したいですか?

+0

考えられる解決策:

<com.android.launcher.Workspace android:id="@+id/workspace" android:layout_width="fill_parent" android:layout_height="fill_parent" launcher:defaultScreen="1"> <include android:id="@+id/cell1" layout="@layout/workspace_screen" /> <include android:id="@+id/cell2" layout="@layout/workspace_screen" /> <include android:id="@+id/cell3" layout="@layout/workspace_screen" /> </com.android.launcher.Workspace> 

そして、あなたはレイアウトパラメータを上書きすることができることを知ってhttp://stackoverflow.com/questions/3421864/how-do-i-access -y-reuse-it-multiple-timesのレイアウト内のビュー – Haozhun

答えて

7

reusing layoutsを参照してください。

+0

@ xevincent、resuedレイアウト内の要素のidを変更する方法bottom.xml、他のレイアウトでbottom.xmlレイアウトを再利用するときにeditTextのidを変更したい、idを変更する方法は? – Leem

+0

xml形式でbottom.xml内の要素のidを変更することはできませんが、Javaで行うことができます(View.setId(int)を参照)。 – xevincent

2

を更新し、このドキュメントの表情を持っています。私はこの答えを追加しました.SOは、「ターゲットサイトに到達できない場合や、永続的にオフラインになる場合に備えて、常に重要なリンクの最も関連性の高い部分を引用する」ことを推奨しています。

したがって、his linkは、<include />を使用する必要があると説明しています。あなたの "更新" に

<include android:layout_width="fill_parent" layout="@layout/image_holder" /> 
関連する問題