2016-03-08 121 views
1

在我的应用程序中,我目前有2项活动。我能够让工具栏显示在第二个工具栏中,但在主要工具栏中,它不会显示在早期的API中。如果我在Marshmallow设备上运行它,它可以正常工作,但我尝试在API16(4.1)的仿真器上运行它,并且工具栏不在那里。我可以打开我的第二个活动,并出现该工具栏,但不是主要的工具栏,即使我将它们实施为完全相同。在我的活动我的xml有:API16中未显示工具栏/操作栏

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:background="#fafafa" 
    tools:context=".Quotr"> 

    <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/main_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      android:elevation="4dp" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

    <include layout="@layout/content_quoter" /> 

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


    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Change colors" 
     android:id="@+id/button" 
     android:visibility="invisible" 
     android:layout_above="@+id/mainFab" 
     android:layout_centerHorizontal="true" 
     android:layout_gravity="bottom" 
     android:layout_marginBottom="50dp"/> 


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

然后在我的onCreate我已经

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

这是我的styles.xml:

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 

<style name="NoActionBarTransparentNav" parent="AppTheme.NoActionBar" > 
    <item name="android:windowTranslucentNavigation">true</item> 
</style> 

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 

如果我忘记发布任何内容了完整的来源是:
https://github.com/Ashanmaril/Quotr

出于某种原因,工具栏将显示在棉花糖和什么,但不是在果冻豆。

回答

0

终于想通了。我不得不将我的工具栏放在LinearLayout中以使其工作

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
<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/main_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:elevation="4dp" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 

而且修复了它。

0

在使用名为.java

public class MainActivity extends Activity 

public class MainActivity extends ActionBarActivity?? 

而且试图迫使

if (android.os.Build.VERSION.SDK_INT > 13) { 
    ActionBar bar = getSupportActionBar(); 
    bar.setLogo(R.drawable.myicon); 
    bar.setDisplayUseLogoEnabled(true); 
    bar.setDisplayShowHomeEnabled(true); 
} 
+0

扩展活动中断setActionBar()和ActionBarActivity已弃用,而不是我正在使用的AppCompatActivity。无论如何,我都投了一票,它没有改变任何东西。 –