简体   繁体   English

无法在主要活动上显示操作栏

[英]Unable to display action bar on main activity

style.xml style.xml

here i purposely disable action bar because i dont want it to appear on my splash screen 在这里我故意禁用动作栏,因为我不希望它出现在我的启动画面上

 <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 MainActivity.java

After that I want action bar exist in Main activity by using set supportactionbar, but the action bar doesnt work after I run the code, and whole process is without error, can someone tell me what's wrong with my code. 之后我想通过使用set supportactionbar在Main活动中存在操作栏,但是在我运行代码后操作栏不起作用,并且整个过程没有错误,有人可以告诉我我的代码有什么问题。

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

You have two options. 你有两个选择。

1: Manually add a Toolbar in your XML. 1:在XML中手动添加工具栏。 Example: 例:

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

See https://guides.codepath.com/android/Using-the-App-Toolbar for more toolbar information. 有关更多工具栏信息,请参阅https://guides.codepath.com/android/Using-the-App-Toolbar

OR 要么

2: Create a new theme for your splash screen and set it in your manifest. 2:为启动画面创建一个新主题并将其设置在清单中。 Example: 例:

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

And then in your app's styles.xml: 然后在你的应用程序的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>

Either one of these are fine, although personally I always keep a separate style for my splash screen just to keep things clean and simple. 其中任何一个都很好,虽然我个人总是为我的闪屏保留一个单独的样式,只是为了保持简洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM