我想在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>
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:
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);
}
请帮帮我
答案 0 :(得分:4)
您需要再创建一个包含工具栏的xml文件: 此xml的名称是工具栏
<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
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
在你的活动中,做这样的事情:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
答案 1 :(得分:2)
如果您在MainActivity XML中使用<include />
,则不应将android:id="@+id/app_bar"
放在标记<android.support.v7.widget.Toolbar />
中。
答案 2 :(得分:1)
我已移除relativeLayout
而只是使用android.support.v7.widget.Toolbar
。并且工作。
答案 3 :(得分:0)
尝试使用相同的指令从https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/下载android_m2repository_r29.zip
存储库。