Android Tabs mit TabLayout und ViewPager

  • Antworten:0
Gelöschter Account
  • Forum-Beiträge: 22

03.10.2015, 11:48:14 via Website

Hi liebes Forum,

ich setze mich gerade mit den Material Tabs aus der Android Design Support Library auseinander. Habe dazu ein Tutorial gefunden. Läuft auch alles soweit, nur habe ich das Problem, das mir zwar die Tabs und der Hintergrund des Linear Layouts aus meinem Fragment angezeigt wird ( benutze das Fragment aus dem Tutorial, ist nur eine Test-App in der ich n bisschen mit UI experimentier ) aber keines der im Layout definierten Elemente

<?xml version="1.0" encoding="utf-8"?>

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/holo_red_dark"
>

<Button
    android:layout_marginTop="29dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:layout_height="match_parent"
    android:text="Fragment"
    android:textColor="#ffffff" />

Für meine MainActivity benutze ich die AppCompatActivity, da in der FragmentActivity ja die Methode setSupportActionBar() nich existiert, ich aber eine Toolbar nutze.

 ViewPager pager = (ViewPager) findViewById(R.id.viewpager);

    pager.setAdapter(new SimpleFragmentAdapter(getSupportFragmentManager(), MainActivity.this));
    TabLayout tabs = (TabLayout) findViewById(R.id.tabLayout);
    tabs.setupWithViewPager(pager);

So sieht der Teil des Setups der Tabs aus.
Und noch das Layout der Main Activity.

<?xml version="1.0" encoding="utf-8"?>

android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".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"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    />

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

Hat jemand eine Idee was der Fehler sein könnte ?

Danke im Vorraus
Lg Moritz

PS: Ich hab die Schema Links aus der XML Source rausnehmen müssen, weil mich das Forum sonst anmotzt, das neue Mitglieder keine Links benutzen dürfen :D Das Tutorial nennt sich "Google Play Style Tabs using TabLayout"

Antworten