2016-06-13 16 views
2

Ich wollte einen dynamischen Inhalt hinzufügen, um alle bereits erstellten Layouts zu nutzen, ohne dass ein XML mit allen Funktionen für jede Aktivität benötigt wird.Wie man ein Layout dynamisch in Android einbaut?

Mir ist aufgefallen, dass der Androide wollte dies verwenden, um zu bestimmen, was mein Inhalt sein wird, je nach der Aktivität zugegriffen.

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="br.com.p21sistemas.certidao21.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include android:id="@+id/content" layout="@layout/content_??????" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

public class WizardActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); // main xml 

     // Something I do not know what may include my content content_wizard 
    } 

} 

Antwort

4

Sie könnten ein leeres Layout in Ihrem XML definieren, wo Sie Sie dynamische Inhalte ist es von Codeinhalt sein und hinzufügen möchten.

Etwas wie folgt aus:

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 
</android.support.design.widget.AppBarLayout> 

<LinearLayout 
    android:id="@+id/dynamic_content" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_dialog_email" /> 

Und dann, aus dem Code:

// get ahold of the instance of your layout 
LinearLayout dynamicContent = (LinearLayout) findViewById(R.id.dynamic_content); 

// assuming your Wizard content is in content_wizard.xml 
View wizardView = getLayoutInflater() 
    .inflate(R.layout.content_wizard, dynamicContent, false); 

// add the inflated View to the layout 
dynamicContent.addView(wizardView); 
Verwandte Themen