2015-04-10 24 views
6

için döküm edilemez ve aşağıdaki linki kullanınız:RelativeLayout ben araç çubuğuna gölge eklemek istediğiniz android.support.v7.widget.Toolbar

: Bana LogCat bu hatayı göstermek ToolBar Shadow

ama çalışan bir uygulama

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

Çubuğu kodu:

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

beni size MainActivity XML <include /> kullanmak

cevap

3

: Bu xml Adı araç olduğunu

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

Şimdi değiştirebilir göreceli düzeni en bu

gibi etiketini dahil böyle

Ve aktivitesinde

<include 
    layout="@layout/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 
, do şey:

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

, sen android:id="@+id/app_bar" koymamaliydiniz yardımcı olabilir, ancak etiketi yerine <android.support.v7.widget.Toolbar /> yılında ediniz. Eğer araç çubuğu içeren bir daha xml dosyası yapmak ihtiyacımız olacak

1

relativeLayout kaldırdım, yalnızca android.support.v7.widget.Toolbar kullanın. Ve işe yarıyor.

İlgili konular