RelativeLayout 无法转换为 android.support.v7.widget.Toolbar
RelativeLayout cannot be cast to android.support.v7.widget.Toolbar
我想给 ToolBar 添加阴影,我使用了下面的 link:
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>
主要活动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"/>
主要活动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);
}
请帮帮我
在 MainActivity XML 中使用 <include />
的地方,不应放置 android:id="@+id/app_bar"
,而应放在标记 <android.support.v7.widget.Toolbar />
.
中
您将需要再制作一个 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);
我删除了 relativeLayout
而只是使用 android.support.v7.widget.Toolbar
。和 ts 工作。
尝试使用相同的说明从 https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/ 下载 android_m2repository_r29.zip
存储库。
我想给 ToolBar 添加阴影,我使用了下面的 link: 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>
主要活动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"/>
主要活动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);
}
请帮帮我
在 MainActivity XML 中使用 <include />
的地方,不应放置 android:id="@+id/app_bar"
,而应放在标记 <android.support.v7.widget.Toolbar />
.
您将需要再制作一个 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);
我删除了 relativeLayout
而只是使用 android.support.v7.widget.Toolbar
。和 ts 工作。
尝试使用相同的说明从 https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/ 下载 android_m2repository_r29.zip
存储库。