7

Ich habe versucht, Toolbar und TabLayout mit Google Design-Bibliothek zu implementieren, indem Sie auf [http://blog.grafixartist.com/material-design-tabs-with-android-design-support-library/] [Blog] verweisen.Toolbar und TabLayout ist nicht auf Android 4.4 Geräten zu sehen

Die Ausgabe funktioniert wie erwartet auf Lollipop-Geräten, zeigt aber keine ToolBar und TabLayout auf Kitkat-Geräten an. Aber ich kann immer noch durch 3 Fragment wie erwartet auf kitkat Gerät wischen. Wie kommt es, dass der gleiche Code, der mit Google Support Libraries geschrieben wurde, auf verschiedenen Geräten anders funktioniert?

Ich versuchte, Bezug auf [Toolbar is not visible on Android 4.X devices [solved] Frage, aber es löste das Problem nicht. Ich habe versucht, den Code im Emulator mit API 19 laufen zu lassen, habe aber auch das gleiche Problem.

Ich habe 'com.android.support:appcompat-v7:22.2.0', 'com.android.support:support-v4:22.2.0' und 'com.android.support:design:22.2.0' Abhängigkeiten im Projekt hinzugefügt.

activity_main.xml

<android.support.design.widget.CoordinatorLayout 
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/tools"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Dark" /> 

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

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

<android.support.v4.view.ViewPager 
    android:id="@+id/viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

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

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    public ViewPager viewPager; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     viewPager = (ViewPager) findViewById(R.id.viewpager); 
     setupViewPager(viewPager); 

     TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout); 
     tabLayout.setupWithViewPager(viewPager); 

     tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       viewPager.setCurrentItem(tab.getPosition()); 
      } 
      @Override 
      public void onTabUnselected(TabLayout.Tab tab) { 
      } 
      @Override 
      public void onTabReselected(TabLayout.Tab tab) { 
      } 
     }); 

    } 

    private void setupViewPager(ViewPager viewPager) { 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
     adapter.addFrag(new QueuedFragment(), "Queued"); 
     adapter.addFrag(new IntransitFragment(), "InTransit"); 
     adapter.addFrag(new DeliveredFragment(), "Delivered"); 
     viewPager.setAdapter(adapter); 
    } 
} 

style.xml

<resources> 

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
        <item name="windowActionBar">false</item> 
        <item name="windowNoTitle">true</item> 
</style> 

+0

Können Sie Ihre Styles-Datei? –

+0

Ich habe eine Style-Datei hinzugefügt. Bitte lassen Sie mich wissen, wenn Sie weitere Informationen benötigen. –

+0

Warum ist 'TabLayout' height' match_parent '? –

Antwort

1
<android.support.design.widget.CoordinatorLayout 
    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/tools"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"//here 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

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

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

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

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

Und:

<resources> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <item name="windowActionBar">false</item> 
     <item name="windowActionBarOverlay">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 
</resources> 

oder:

<style name="ParallaxTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 
+1

Dank SilentKnight nach windowActionBarOverlay Eigenschaft innerhalb style.xml Datei hinzufügen, es funktioniert jetzt gut. –

0

Wenn das oberste Layout-Elternelement in activity_main.xml kein CoordinatorLayout ist (gibt es FrameLayout oder RelativeLayout?), Kann es je nach API-Version zu unterschiedlichen Überlappungen kommen. Ändern Sie Top-Layout-Eltern zu einem CoordinatorLayout und es wird überall funktionieren.

+0

Top-Layout ist nur CoordinatoLayout, aber aufgrund eines Bearbeitungsproblems wurde es nicht früher angezeigt.Könnten Sie es bitte jetzt überprüfen und lassen Sie mich wissen, wenn etwas nicht stimmt –

+0

Änderung 'xmlns: app =" http://schemas.android.com/tools "' zu 'xmlns: app =" http://schemas.android .com/apk/res-auto „' – GPack

+0

ich es auf ‚xmlns: app =‘ http://schemas.android.com/apk/res-auto“, aber immer noch war ich das gleiche Problem konfrontiert und nach der Zugabe von„< . item name = „windowActionBarOverlay“> false "-Linie innerhalb style.xml Datei, wie SilentKnight vorgeschlagen, es funktioniert jetzt gut –

1

Ich hatte Problem in Android 4 und ich es gelöst. Siehe wie. Sie müssen die Symbolleiste auf eine höhere Ebene verschieben.


<?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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    > 
    <!-- Framelayout to display Fragments --> 
    <FrameLayout 
     android:id="@+id/frame_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <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="@android:color/transparent" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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



    <!--<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" 
     app:srcCompat="@android:drawable/ic_dialog_email" />--> 

</android.support.design.widget.CoordinatorLayout> 
1

dies mir passiert ist, und es war, weil ich eine CoordinatorLayout wurde mit aber ich war nicht die layout_behavior des Inhalts angeben. Also sollten Sie dies in meinem Fall tun:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/cl_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white" 
    tools:ignore="MissingPrefix"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:contentScrim="@color/white" 
      app:titleEnabled="false"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="@android:color/white" 
       android:elevation="4dp" 
       app:layout_collapseMode="pin"> 

       <TextView 
        android:id="@+id/toolbar_title" 
        fontPath="fonts/Medium-Extd.otf" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="my toolbar title" 
        android:textAllCaps="true" 
        android:textColor="#000000" 
        android:textSize="14sp" /> 

      </android.support.v7.widget.Toolbar> 
     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 


<!-- add the behavior here and the toolbar apppeared on 4.4 devices in my case --> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" > 

//...... blah blah 

     </RelativeLayout>