2016-01-15 22 views
0

我想替代一個工具欄而不是一個ActionBar。這裏是我的styles.xml內容試圖提供一個工具欄而不是一個ActionBar

<resources> 
... 
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowActionBar">false</item> 
</style> 
... 
</resources> 

這裏是我的清單。

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/MyTheme"> 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/MyTheme"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN"/> 

      <category android:name="android.intent.category.LAUNCHER"/> 
     </intent-filter> 

    </activity> 
    <service android:name=".CustomFloating" android:label="Floating Class"/> 
</application> 

而且,這裏是佈局,我希望實現這種風格。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="sports.scores.floating.MainActivity"> 
<android.support.v7.widget.Toolbar 
    android:id="@+id/my_awesome_toolbar" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:title="@string/app_name" 
    android:minHeight="?attr/actionBarSize" 
    style="@style/MyTheme"/> 

<include layout="@layout/content_main"/> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_dialog_email"/> 

</android.support.design.widget.CoordinatorLayout> 

我面對所有的佈局文件相同的錯誤,雖然我的windowActionBar的值設置爲false。這是我的MainActivity類的內容。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); 
    setSupportActionBar(toolbar);//line number 34 
    ... 
} 

但是,我得到這個堆棧跟蹤。

Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. 
     at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198) 
     at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99) 
     at sports.scores.floating.MainActivity.onCreate(MainActivity.java:34) 

有人可以幫助我這個。我希望用my_awesome_toolbar替代此操作欄。

+1

如果您提供自定義工具欄 –

+0

<樣式名稱= 「MyTheme的」 父= 「Theme.AppCompat.Light.NoActionBar」> –

+0

BT我應該使用無操作欄主題已將windowActionbar屬性設置爲false。它是什麼意思,如果我使用NoActionBar –

回答

3

修改樣式

<resources> 
... 
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar"> 

</style> 
... 
</resources> 
+0

這不工作。我仍然收到同樣的錯誤。 –

+0

我把我的話回來。這工作。但現在,如何更改我的工具欄的顏色? –

+1

加入你的xml:android:background =「@ android:color/transparent」 –

相關問題