【问题标题】:Action bar / toolbar not shows in below Lollipop version操作栏/工具栏未显示在以下 Lollipop 版本中
【发布时间】:2016-07-15 06:31:19
【问题描述】:

我从 android studio 活动库中创建了活动。

以下是layout.xml文件的代码

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.atgarage.Home.HomeActivity">

    <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="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <!--<include layout="@layout/content_home" />-->

    <FrameLayout
        android:id="@+id/mainfragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

当我运行应用程序时,操作栏不显示在 Lollipop 下方,但当我在 Lollipop 上方运行时,它完美显示。

以下是应用风格。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowNoTitle">true</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="windowNoTitle">true</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowFullscreen">true</item>
    </style>

任何人都可以有解决方案。

提前致谢。

【问题讨论】:

    标签: android android-layout android-actionbar toolbar


    【解决方案1】:

    改变这个

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="android:windowNoTitle">true</item>
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="windowNoTitle">true</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
            <item name="android:windowFullscreen">true</item>
        </style>
    

    到这里

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- 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>
    

    更新日期:

    manifest.xml 文件

    <activity
                android:name=".HomeActivity"
                android:label="@string/app_name"
     //add this     android:theme="@style/AppTheme.NoActionBar">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    

    【讨论】:

    • 我已经改变了这个......但仍然面临同样的问题
    • @Naitik 你有没有在manifest.xml 文件中设置这个android:theme="@style/AppTheme.NoActionBar" 这个样式或者发布你的manifest.xml 文件..
    【解决方案2】:

    它使用向片段容器布局添加上边距来修复。

    <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.atgarage.Home.HomeActivity">
    
        <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="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    
        </android.support.design.widget.AppBarLayout>
    
        <!--<include layout="@layout/content_home" />-->
    
        <FrameLayout
            android:id="@+id/mainfragment"
            android:layout_width="match_parent"
            android:layout_marginTop="?attr/actionBarSize"
            android:layout_height="match_parent" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

      【解决方案3】:

      你应该添加 -

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

      在你 Activity 的 onCreate() 方法中。

      【讨论】:

      • 我猜你的框架布局在较低的 API 上覆盖了整个屏幕
      【解决方案4】:

      您的活动应该扩展 AppCompatActivity 而不是 Activity 或其他东西。然后你就可以使用了

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

      编辑:

      首先,您需要使用content_home.xml 并将所有内容放在那里。这是一种常见的做法,谷歌将其设为默认值,这意味着它背后有一些原因。因此,您需要将您的 FrameLayout 移动到 content_home.xml

      在您的情况下,我尝试通过创建新项目并在 API 19 设备上对其进行测试来进行测试。它适用于以下设置:

      AndroidManifest.xml

      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.marat.testingapp">
      
          <application
              android:allowBackup="true"
              android:icon="@mipmap/ic_launcher"
              android:label="@string/app_name"
              android:supportsRtl="true"
              android:theme="@style/AppTheme">
              <activity
                  android:name=".MainActivity"
                  android:label="@string/app_name"
                  android:theme="@style/AppTheme.NoActionBar">
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />
      
                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
              </activity>
          </application>
      
      </manifest>
      

      activtiy_main.xml

      <?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"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true"
          tools:context="com.example.marat.testingapp.MainActivity">
      
          <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="?attr/colorPrimary"
                  app:popupTheme="@style/AppTheme.PopupOverlay" />
      
          </android.support.design.widget.AppBarLayout>
      
          <FrameLayout
              android:id="@+id/frameLayout"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
      
      </android.support.design.widget.CoordinatorLayout>
      

      styles.xml

      <resources>
      
          <!-- Base application theme. -->
          <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
              <!-- 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="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
      
          <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
      
      </resources>
      

      MainActivity.java

      public class MainActivity extends AppCompatActivity {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
              setSupportActionBar(toolbar);
          }
      }
      

      将您的代码与此代码进行比较。

      【讨论】:

      • 我也写过上面的代码。但仍然无法正常工作
      • @Naitik 我的回答对你有帮助吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 2015-05-09
      相关资源
      最近更新 更多