android - 为什么工具栏消失了?
问题描述
我实在想不通,为什么我的Toolbar
消失了,应该是哪里出了问题。如您所见,工具栏在那里,但不知何故看起来像透明的。
运行后应用程序中存在相同的错误,但完全不可见。
由于 Navigation Drawer,我在 MainActivity 中使用了自定义工具栏:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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:id="@+id/drawer_layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</LinearLayout>
<include
android:id="@+id/content_main"
layout="@layout/content_main" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view_main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:itemIconTint="@color/colorAccent"
app:itemTextColor="@color/colorPrimary"
app:menu="@menu/drawer_menu_main" />
</androidx.drawerlayout.widget.DrawerLayout>
我的 MainActivity 类看起来像这样:
private lateinit var drawerLayout: DrawerLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar: Toolbar = findViewById(R.id.toolbar_main)
setSupportActionBar(toolbar)
drawerLayout = findViewById(R.id.drawer_layout_main)
val navView: NavigationView = findViewById(R.id.nav_view_main)
val toggle = ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
navView.setNavigationItemSelectedListener(this)
}
另外,我使用自定义样式,NoActionBar 定义为:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
当然在 Manifest 中应用了这个特定的活动:
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
首先,我想使用一个不透明的工具栏,其次,我以前工作过,但是在编程过程中我注意到我不再工作/出现了。我该如何解决这个问题?
解决方案
找到了解决方案。请参考上面 MikeM. 的评论。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<include
android:id="@+id/content_main"
layout="@layout/content_main" />
</LinearLayout>
推荐阅读
- vim - Syntastic 不使用新插件执行语法检查
- java - 从片段(A)的适配器导航到另一个片段(B)时无法解析上下文或应用程序
- bash - 如何使用管道运行程序并终止程序
- android - 以编程方式从 xml 代码中获取 Gridview 项
- python - Django 的 CORS 问题,缺少“Access-Control-Allow-Headers”
- python - 如何根据子目录获取不同的文件路径列表?
- c++ - 使用指针写入数组元素的问题
- android - 如何在 Android Studio 中获取最新的通话记录?
- python - 在 pandas 数据框中的两列中删除包含重复值的行
- php - 将自定义 REST api 登录与另一个网站集成