我想给 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/