java - RelativeLayout 无法转换为 android.support.v7.widget.Toolbar

标签 java android layout

我想给 ToolBar 添加阴影,我使用以下链接: ToolBar Shadow

但是正在运行的应用程序在 LogCat 中显示了这个错误:

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.v7.widget.Toolbar

工具栏代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp">

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    app:theme="@style/MyCustomToolbarTheme"
    android:background="@drawable/main_header">

    </android.support.v7.widget.Toolbar>

主要 Activity XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar_main"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/app_bar"
    android:text="ytkbhjk"/>

主要 Activity Java:

public class MainActivity extends ActionBarActivity {

private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
}

请帮帮我

最佳答案

您将需要再制作一个包含您的工具栏的 xml 文件: 此 xml 的名称是 toolbar

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"/>

现在像这样改变你的相对布局的include标签

<include
    layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

在你的 Activity 中,做这样的事情:

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

关于java - RelativeLayout 无法转换为 android.support.v7.widget.Toolbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29564313/

相关文章:

java - 为什么与 jclouds 一起使用的 CloudWatch API getMetricStatisticsInRegion 会抛出 UndeclaredThrowableException?

java - 通过键盘在 JTable 中按下 JButton

java - 我的 vector3 类中的线程 "AWT-EventQueue-0"java.lang.NullPointerException 中出现异常

java - 单例返回多个实例

java - 如何处理android中的长按

java - 格式化日历事件的日期时间对象,从谷歌日历API检索

java - 在eclipse中将库导入到现有项目会造成很多麻烦

android - Android 应用程序中的选项菜单布局

java - 三星 Galaxy Y S5360 的布局问题

android - 我必须向布局添加什么才能隐藏标题栏?