0

我按照這個教程http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html。我有依賴正確的,添加到了我的佈局文件toolbar.xml將動作欄更改爲工具欄,現在我正在關閉力量。我在這裏做錯了什麼?

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
android:id="@+id/drawer_layout" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/simplecolor" /> 
    </LinearLayout> 
</android.support.v4.widget.DrawerLayout>  

而這主要活動:

import android.support.v7.widget.Toolbar; 

public class MainActivity extends ActionBarActivity { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
} 

什麼我錯在這裏做什麼?爲什麼我要關閉一支部隊?它與正常的操作欄一起工作良好,但在將其更改爲工具欄後,我在應用程序啓動時遇到了強制關閉。我是新的android編碼,請幫助..

回答

0

你的代碼是好的。但是你錯過了app:theme來設置工具欄作爲操作欄。

<android.support.v7.widget.Toolbar 
    android:layout_height=」wrap_content」 
    android:layout_width=」match_parent」 
    android:minHeight=」@dimen/triple_height_toolbar」 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

變化styles.xml以下

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar"> 

    <item name="windowActionBar">false</item> 

</style> 
相關問題