2015-04-10 6 views
6

ich Schatten hinzufügen möchten Symbol- und ich verwende den folgenden Link: ToolBar ShadowRelativeLayout kann nicht auf android.support.v7.widget.Toolbar gegossen werden

aber läuft app mir diesen Fehler in LogCat zeigen:

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

Toolbar Code:

<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); 
} 

bitte helfen Sie mir

Antwort

3

Sie gehen zu müssen, eine weitere XML-Datei machen, die Ihre Symbolleiste enthält: Namen dieses xml ist 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"/> 

Jetzt ist Ihr relatives Layout ändern der umfassen Tag wie diese

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

Und in Ihrer Aktivität, tun Sie etwas wie das:

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

Wo Sie die <include /> in Ihrem MainActivity XML verwenden, sollten Sie nicht die android:id="@+id/app_bar" setzen, sondern in dem Tag <android.support.v7.widget.Toolbar />.

1

Ich habe relativeLayout entfernt, stattdessen einfach android.support.v7.widget.Toolbar verwenden. Und ts arbeiten.

Verwandte Themen