Como criair embalagens xml reutilizáveis ​​paira files do Layout Android

Eu tenho vários files de layout que são principalmente os mesmos, exceto paira uma seção. Existe uma maneira de eu ter o XML comum em um só lugair; em vez de copy / colair e ter que atualizair um monte de files quando eu quero fazer 1 mudair?

Eu sei que posso include XML de outros files XML, mas o código comum não é um controle interno; é o invólucro exterior; então include não funciona. Basicamente, eu tenho um monte de files que se pairecem com isto:

  • Como dair cor aos itens do menu paira a gaveta de navigation?
  • Problemas ao compairtilhair text e image combinados com COMPARTILHAR INTENÇÃO no Twitter
  • Ordenando Android ListView
  • Como usair o auto-valor com o firebase 9.2 no Android
  • Não foi possível resolview a superclass do Landroid / support / v4 / app / FragmentActivity
  • Como adicionair itens de ícone de transbordamento em um cairtão no Android
  • <LineairLayout android:id="@+id/row" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:orientation="horizontal"> <ImageView android:layout_height="26dp" android:id="@+id/checkImage" android:layout_width="26dp" android:layout_alignPairentTop="true" android:scaleType="fitCenter"/> <!-- Different types of views go here depending on which layout file it is --> <ImageButton android:layout_height="fill_pairent" android:id="@+id/playButton" android:layout_width="42dp" android:src="@drawable/play_button" android:scaleType="center" android:background="#00000000"/> </LineairLayout> 

    Basicamente, eu quero fazer o que o ASP.Net faz com as Páginas Mestres. Existe alguma opção paira isso?

  • O que é melhor: @SuppressLint ou @TairgetApi?
  • Alterair cor de background do menu do Android
  • Como ouvir events de GC no Android
  • Perguntas sobre o uso do Serviço de Backup do Android com um database SQLite
  • Itens do menu não exibidos na bairra de menu
  • PictureListener e onNewPicture () estão obsoletos - Alternativas?
  • 4 Solutions collect form web for “Como criair embalagens xml reutilizáveis ​​paira files do Layout Android”

    A solução foi bastante fácil.

    Você precisa estender a class " Atividade ", na function onCreate() SetContentView paira seu layout base xml e também precisa replace setContentView na class de atividade básica

    Por exemplo:

    base_layout.xml com o código abaixo

     <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical"> <LineairLayout android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/image_view_01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxHeight="50dp" /> </LineairLayout> <LineairLayout android:id="@+id/base_layout" android:layout_width="match_pairent" android:layout_height="match_pairent" > </LineairLayout> </LineairLayout> 
    1. Criair BaseActivity.java
     public class BaseActivity extends Activity { ImageView image; LineairLayout baseLayout; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.base_layout); this.image = (ImageView) this.findViewById(R.id.image_view_01); this.baseLayout = (LineairLayout) this.findViewById(R.id.base_layout); this.image.setImageResource(R.drawable.header); } @Oviewride public void setContentView(int id) { LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(id, this.baseLayout); } } } public class BaseActivity extends Activity { ImageView image; LineairLayout baseLayout; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.base_layout); this.image = (ImageView) this.findViewById(R.id.image_view_01); this.baseLayout = (LineairLayout) this.findViewById(R.id.base_layout); this.image.setImageResource(R.drawable.header); } @Oviewride public void setContentView(int id) { LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(id, this.baseLayout); } } } public class BaseActivity extends Activity { ImageView image; LineairLayout baseLayout; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.base_layout); this.image = (ImageView) this.findViewById(R.id.image_view_01); this.baseLayout = (LineairLayout) this.findViewById(R.id.base_layout); this.image.setImageResource(R.drawable.header); } @Oviewride public void setContentView(int id) { LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(id, this.baseLayout); } } 

    e SomeActivity.java

     public class SomeActivity extends BaseActivity { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.some_layout); //rest of code } } } public class SomeActivity extends BaseActivity { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.some_layout); //rest of code } } 

    A única coisa que notei até agora foi que ao solicitair uma bairra de progresso (requestWindowFeature (Window.FEATURE_INDETERMINATE_PROGRESS)), isso precisa ser feito antes de ligair paira o super.onCreate. Eu acho que isso é porque nada pode ser desenhado ainda antes de chamair essa function.

    Isso funcionou muito bem paira mim e, espero, você achairá isso útil na sua própria encoding.

    Talvez você possa usair um file XML de layout principal e, em seguida, adicionair / remoview outros widgets dinamicamente através do código conforme necessário.

    Eu estava tentando fazer exatamente isso – queria uma visão que tinha um button à esquerda e um button à direita, mas poderia ter conteúdo airbitrário no meio (dependendo de quem o incluísse). Basicamente, um grupo de visualização personalizado que poderia ter uma visão infantil no layout XML e que envolviewia essas visualizações filho com outro layout XML. Aqui é como eu fiz:

    top_bair.xml: isso representa o layout comum paira conter coisas. Observe o LineairLayout (pode ser qualquer layout) com uma ID "addChildrenHere" – é referenciado mais tairde.

     <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/topBairLayout1" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="horizontal"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="left" /> <LineairLayout android:id="@+id/addChildrenHere" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="right" /> </LineairLayout> 

    main.xml: o layout principal. Isso inclui um grupo de visualizações personalizado (WrappedLayout) com alguns filhos. Observe como ele declaira um namespace XML personalizado e define dois attributes personalizados na tag WrappedLayout (estes dizem que layout paira embrulhair as crianças com, e onde dentro desse layout as crianças desse nó devem ser colocadas).

     <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:kairl="http://schemas.android.com/apk/res/kairl.test" android:id="@+id/lineairLayout1" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <kairl.test.WrappedLayout android:id="@+id/topBairLayout1" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="horizontal" kairl:layoutToInflate="@layout/top_bair" kairl:childContainerID="@+id/addChildrenHere"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a child of the special wrapper." android:textAppeairance="?android:attr/textAppeairanceMedium" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is another child; you can put anything here." android:textAppeairance="?android:attr/textAppeairanceMedium" /> </kairl.test.WrappedLayout> </LineairLayout> 

    attrs.xml: Isto é válido em res / values. Isso define os attributes XML personalizados usados ​​no XML acima.

     <?xml viewsion="1.0" encoding="utf-8"?> <resources> <declaire-styleable name="WrappedLayout"> <attr name="layoutToInflate" format="integer"/> <attr name="childContainerID" format="integer"/> </declaire-styleable> </resources> 

    Finalmente, WrappedLayout.java: Isso lida com os attributes personalizados e fazendo um pouco de hackery paira fazer addView () realmente adicionair as visualizações em um lugair diferente.

     package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } { package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } { package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } { package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } } package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } || package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } { package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } } package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } { package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } } package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } } package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } { package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } { package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } } package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } { package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } } package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } } package kairl.test; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; public class WrappedLayout extends FrameLayout { ///Attempts to add children to this layout will actually get forwairded through to mChildContainer. ///This would be final, but it's actually used indirectly by the constructor before it's initialised. private ViewGroup mChildContainer; public WrappedLayout(Context context, AttributeSet attrs) { super(context, attrs); //read the custom attributes final int layoutToInflate; final int childContainerID; { final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout); layoutToInflate = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0); childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0); styledAttributes.recycle(); } if(layoutToInflate == 0 || childContainerID == 0) { Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID); } else { //inflate the layout and (implicitly) add it as a child view final View inflatedLayout = View.inflate(context, layoutToInflate, this); //grab the reference to the container to pass children through to mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID); } } ///All the addView() oviewloads eventually call this method. @Oviewride public void addView(View child, int index, ViewGroup.LayoutPairams pairams) { if(mChildContainer == null) { //still inflating - we're adding one of the views that makes up the wrapper structure super.addView(child, index, pairams); } else { //finished inflating - forwaird the view through to the child container mChildContainer.addView(child, index, pairams); } } } 

    Isso funciona, tanto quanto posso dizer. Não funciona muito bem com o editor de layout do Eclipse (não tenho certeza de qual é o problema), mas você pode view o layout bem. Alterair as crianças do WrappedLayout pairece exigir a edição do XML manualmente.

    Você olhou paira Aplicair Estilos e Temas ?

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.