2017-07-10 6 views
-1

Wie kann ich ein Layout zu einem anderen Layout hinzufügen?So fügen Sie ein Layout zu einem anderen Layout in Android hinzu

Wie kann ein in Java geschriebenes Layout zu einem anderen Layout hinzugefügt werden. In diesem Fall möchte ich new RevAppBarLayout().getRevAppBarLayout(this); zu einem DrawerLayout namens Schublade hinzufügen.

DrawerLayout drawer = new DrawerLayout(this); 
drawer.addView(inflatedLayout); 

public class RevAppBarLayout extends MainActivity { 

    public AppBarLayout getRevAppBarLayout(Context context) { 
     AppBarLayout revAppBarLayout = new AppBarLayout(context); 
     Toolbar revToolBar = new Toolbar(context); 

     setSupportActionBar(revToolBar); 

     return revAppBarLayout; 
    } 
} 

Dies ist, wie es zu tun ist, wenn es sich um ein XML-Fragment waren:

AppBarLayout revAppBarLayout = new RevToolBar().getRevAppBarLayout(this); 

LayoutInflater inflater = LayoutInflater.from(context); 
View inflatedLayout= inflater.inflate(R.layout.yourLayout, null, false); 
drawer.addView(inflatedLayout); 
+0

Warum ist ein Fragment wichtig? 'View.addView' ist die Methode, und Sie scheinen sie bereits zu benutzen –

Antwort

1

Sie Schublade zu Ihrem Layout hinzufügen Sie? Da es nicht von Ihrem Layout abgerufen wird, instanziieren Sie es dynamisch als neues Objekt.

Und Sie brauchen keinen Inflater, um Layout oder Ansicht zu einem anderen Layout hinzuzufügen. Wenn es Aktivität ist, rufen Sie das Layout (RelativeLayout, FrameLayout usw.) mit findViewById(R.id.layoutContainer); auf und fügen Sie dann mithilfe von addView eine beliebige Ansicht zu diesem Layout hinzu.

Verwandte Themen