Other answers have given some alternatives, but I think I can add one more.
My suggestion would be to create a single layout as a real template and use ViewStub
to define the dynamic content of this template when creating% .
For example:
template.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<!-- Aqui entra seu topo -->
<include ... />
<!-- O ViewStub guarda lugar para um layout
a ser definido por quem usar -->
<ViewStub android:id="@+id/stub"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weight="1" />
<!-- Aqui entra seu fundo -->
<include ... />
</LinearLayout>
To dynamically change the layout while creating the Activity's
, just do:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Pode "setar" normalmente seu layout
setContentView(R.layout.template);
ViewStub vs = findViewById(R.id.stub);
// Define qual o layout a ser inflado
vs.setLayoutResource(R.layout.seuLayoutAtual);
View conteudoPrincipal = vs.inflate();
}
Remembering that Activity
will exit the tree and will give way to every subtree that is defined in the layout that you pass in ViewStub
method. The ViewStub.setLayoutResource
method returns the root of the layout you inflated.
This way you can reuse the same layout just by changing the content you need.
For the function question / behaviors that belong to the template , you can create a class, which can be abstract if you prefer, inheriting from some subclass or itself inflate
( Activity
FragmentActivity
and so on) that contains this behavior that is common to all.
You would have something like:
public abstract class BaseActivity extends Activity {
// Template method para retornar o conteudo
// interno ao template.
// Sendo abstrato para obrigar que a subclass forneca
public abstract @LayoutRes int getContentLayoutId();
// Metodo responsavel por configurar o conteudo do template
public abstract void onCreateContent(View conteudo);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.template);
ViewStub vs = findViewById(R.id.stub);
// Define qual o layout a ser inflado
vs.setLayoutResource(getContentLayoutId());
View conteudoPrincipal = vs.inflate();
// Configuracao de listener de eventos e etc
// Deixa a subclasse configurar o conteudo
onCreateContent(conteudoPrincipal);
}
// Metodos comuns
}
Where each%
public class ActivityLoja extends BaseActivity {
@Override
public int getContentLayoutId() {
return R.layout.loja;
}
@Override
public void onCreateContent(View conteudo) {
// Configuracao do conteudo
}
// Restante do codigo da sua Activity
}
public class ActivityCarrinho extends BaseActivity {
@Override
public int getContentLayoutId() {
return R.layout.carrinho;
}
@Override
public void onCreateContent(View conteudo) {
// Configuracao do conteudo
}
// Restante do codigo da sua Activity
}
The registry is normal, as if there were no inheritance:
<activity
android:name=".activity.ActivityLoja"
android:label="@string/loja" />
<activity
android:name=".activity.ActivityCarrinho"
android:name="@string/carrinho" />
Some references: