2017-04-27 68 views
1

style.xml无法在主要活动显示操作栏

在这里,我故意禁止行动起来吧,因为我不希望它出现我的启动画面上

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

MainActivity.java

后我想通过使用set supportactionbar在主要活动中存在操作栏,但在运行代码后操作栏不起作用,并且整个过程没有错误,有人能告诉我我的代码出了什么问题。

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

回答

0

您有两种选择。

1:在XML中手动添加工具栏。示例:

<LinearLayout 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" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:minHeight="?attr/actionBarSize" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:titleTextColor="@android:color/white" 
     android:background="?attr/colorPrimary"> 
    </android.support.v7.widget.Toolbar> 

</LinearLayout> 

有关更多工具栏信息,请参见https://guides.codepath.com/android/Using-the-App-Toolbar

OR

2:闪屏创建一个新的主题,并设置它在你的清单。例如:

<activity 
     android:name=".view.activity.SplashActivity" 
     android:theme="@style/Splash"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN"/> 
      <category android:name="android.intent.category.LAUNCHER"/> 
     </intent-filter> 
    </activity> 

然后在你的应用程序的styles.xml:

<style name="Splash" parent="Theme.AppCompat.NoActionBar"> 
    <item name="android:windowFullscreen">true</item> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="android:windowBackground">@drawable/splash_screen</item> 
</style> 

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

无论是其中的一个都很好,虽然我个人始终保持一个单独的样式给我闪屏只是为了保持清洁,简单。