RelativeLayout无法强制转换为android.support.v7.widget.Toolbar [英] RelativeLayout cannot be cast to android.support.v7.widget.Toolbar

查看:81
本文介绍了RelativeLayout无法强制转换为android.support.v7.widget.Toolbar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to add shadow to ToolBar and I use the following link: ToolBar Shadow

但正在运行的应用程序在LogCat中显示此错误:

but running app show me this error in 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>

MainActivity XML:

MainActivity 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"/>

MainActivity Java:

MainActivity 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的名称是工具栏

You are going to need to make one more xml file which contains your toolbar: Name of this xml is 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 标签

Now change your relative layout's include tag like this

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

在您的活动中,请执行以下操作:

And in your activity, do something like this:

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

这篇关于RelativeLayout无法强制转换为android.support.v7.widget.Toolbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆