2014-12-19 86 views
0

語境工具欄問題的材料設計與「舊」版本

我跟着一些例子來創建與材料設計,並在與SKD + 14,同時retrocompatible一個簡單的應用程序。

所以我創建:

values/theme.xml 
values-21/theme.xml 

值/的themes.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <style name="AppTheme.Base" parent="Theme.AppCompat"> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimary</item> 
     <item name="android:windowNoTitle">true</item> 
     <item name="windowActionBar">false</item> 
    </style> 
</resources> 

值-V21 /的themes.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="AppTheme" parent="AppTheme.Base"> 
     <item name="android:windowContentTransitions">true</item> 
     <item name="android:windowAllowEnterTransitionOverlap">true</item> 
     <item name="android:windowAllowReturnTransitionOverlap">true</item> 
     <item name="android:windowSharedElementEnterTransition">@android:transition/move</item> 
     <item name="android:windowSharedElementExitTransition">@android:transition/move</item> 
    </style> 
</resources> 

MainActivity

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     if (toolbar != null) { 
      setSupportActionBar(toolbar); 
     } 
    } 

main_layout

<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" 
       android:paddingLeft="@dimen/activity_horizontal_margin" 
       android:paddingRight="@dimen/activity_horizontal_margin" 
       android:paddingTop="@dimen/activity_vertical_margin" 
       android:paddingBottom="@dimen/activity_vertical_margin" 
       tools:context=".MainActivity"> 

    <TextView 
     android:text="@string/hello_world" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <include 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     layout="@layout/toolbar" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true"/> 

</RelativeLayout> 

** toolbar.xml *

<?xml version="1.0" encoding="utf-8"?> 
<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/colorPrimaryDark"/> 

問題

在與Android 5.0的手機完美,但在與4.4.4的手機,這些錯誤是推出: ,我不知道如何解決它。

Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. 
      at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:165) 
      at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92) 
      at com.shudy.loteriadenavidad.MainActivity.onCreate(MainActivity.java:18) 
      at android.app.Activity.performCreate(Activity.java:5231) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5139) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) 
            at dalvik.system.NativeStart.main(Native Method) 

回答

0

如果包含了工具欄在你的佈局,你必須與.NoActionBar

<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimary</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="windowActionBar">false</item> 
</style> 
主題