【发布时间】:2021-06-23 21:40:25
【问题描述】:
我有一个toolbar.xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_primary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark" />
我的另一个活动布局有一个包含标签:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/AppTheme">
<include
layout="@layout/toolbar"
android:id="@+id/tb">
</include>
<android.support.v7.widget.RecyclerView
android:id="@+id/grade_list"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
我已经在我的扩展 AppCompatActivity 的活动中实现了它:
Toolbar toolbar;
toolbar = (Toolbar)findViewById(R.id.tb);
setSupportActionBar(toolbar);
但是,当我运行我的应用程序时出现以下错误:
Error:(26, 29) error: incompatible types: android.widget.Toolbar cannot be converted to android.support.v7.widget.Toolbar
此外,当我在编辑器中查看布局时,我收到以下消息:
Missing styles. Is the correct theme chosen for this layout?
Use the theme combo box above the layout to choose a different layout, or to fix the theme style references.
Failed to find style 'toolbarStyle' in current layout.
还有我的styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/color_primary</item>
<item name="colorPrimaryDark">@color/color_primaryDark</item>
<item name="colorAccent">@color/color_accent</item>
<item name="colorControlNormal">@color/color_accent</item>
</style>
我看不出哪里出错了。在编辑器和我的清单文件中选择了正确的主题。一如既往地感谢所有帮助!
【问题讨论】:
-
在您的活动顶部必须有
import android.support.v7.widget.Toolbar;而不是import android.widget.Toolbar;