7

我試圖通過參考[http://blog.grafixartist.com/material-design-tabs-with-android-design-support-library/] [blog]]使用谷歌設計庫實現工具欄和TabLayout。工具欄和TabLayout在Android 4.4設備上不可見

輸出按預期在棒棒糖設備上工作,但它不會在Kitkat設備上顯示ToolBar和TabLayout。但是我仍然可以在kitkat設備上通過3段片段掃描。如何使用谷歌支持庫編寫的相同代碼在不同設備上的工作方式不同!

我試過參考[Toolbar is not visible on Android 4.X devices [solved]問題,但它沒有解決問題。我試着用API 19在模擬器中運行代碼,但也面臨同樣的問題。

我在項目中添加了'com.android.support:appcompat-v7:22.2.0','com.android.support:support-v4:22.2.0''com.android.support:design:22.2.0'依賴關係。

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

你可以發佈你的樣式文件嗎? –

+0

我已添加樣式文件。如果您需要更多信息,請告訴我。 –

+0

爲什麼'TabLayout'高度'match_parent'? –

回答

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> 

和:

<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> 

或:

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

謝謝SilentKnight在style.xml文件中添加windowActionBarOverlay屬性後,它現在工作正常。 –

0

如果頂層佈局父項activity_main.xml不是CoordinatorLayout(是否存在FrameLayout或RelativeLayout?),則根據API版本可能會有一些錯誤的不同重疊。 將頂層佈局父級更改爲CoordinatorLayout,它將在任何地方都可以使用。

+0

頂部佈局僅爲CoordinatoLayout,但由於某些編輯問題,它沒有提前顯示。請你現在檢查一下,如果有什麼不對,請告訴我 –

+0

更改'xmlns:app =「http://schemas.android.com/tools」'到'xmlns:app =「http://schemas.android .com/apk/res-auto「' – GPack

+0

我將它改爲」xmlns:app =「http://schemas.android.com/apk/res-auto」但我仍面臨同樣的問題,並在添加「 item name =「windowActionBarOverlay」> false「line in style.xml文件中,正如SilentKnight所建議的,現在工作正常。 –

1

我的Android 4.0有問題,我解決它。怎麼看。 需要將工具欄移動到更高級別。


<?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

這發生在我身上,這是因爲我使用的是CoordinatorLayout,但我不指定layout_behavior內容。所以你應該在我的情況下這樣做:

<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> 
相關問題