Erweiterung aller Aktivitäten von einer gemeinsamen Aktivität, die nicht funktioniert
Ich habe einapplication
das hat vieractivities
. Alle diese Aktivitäten sollten aus einem @ bestehetoolbar
oben und einTabLayout
unten. DasTabLayout
hat 4 Registerkarten. Klicken Sie auf ein bestimmtestab
nimmt an einer der vier Aktivitäten teil.
Jetzt möchte ich den Code wie folgt implementieren
1) Erstellen Sie eine übergeordnete Aktivität. Die Aktivität bläst ein Layout auf, das das TabLayout enthält.
2) Alle anderen vier Aktivitäten sollten die übergeordnete Aktivität erweitern. Diese Aktivitäten haben ihre eigenen XML-Layouts.
ParentActivity.java
public class ParentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parent);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
activity_parent.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<android.support.design.widget.TabItem
android:text="One"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<android.support.design.widget.TabItem
android:text="Two"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<android.support.design.widget.TabItem
android:text="Three"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<android.support.design.widget.TabItem
android:text="Four"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</android.support.design.widget.TabLayout>
</RelativeLayout>
Wenn ich OneActivity starte, kann ich die Symbolleiste und das Tablayout, die ich in ParentActivity erstellt habe, nicht abrufen. Ich erhalte nur den Inhalt, den ich in der XML-Datei von OneActivity deklariert habe.
Meine erwartete Benutzeroberfläche ist diese
Das bekomme ich nach dem Start.