2016-07-18 74 views
1

我想使用自定義操作欄,以便在我的應用程序中設置自定義操作欄,但是當我無法運行app.app強制停止時。 我在res/layout中製作一個工具欄,並禁用默認操作欄,然後在MainActivity中設置自定義操作欄。 這是我toolbar.xml:自定義操作欄停止我的應用程序

<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/toolbar" 
android:layout_width="match_parent" 
android:layout_height="?attr/actionBarSize" 
android:background="?attr/colorPrimary" 
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 
<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <!-- This is a centered logo --> 
    <ImageView 
     android:id="@+id/toolbar_logo" 
     android:src="@drawable/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="?attr/actionBarSize" 
     android:layout_marginTop="4dp" 
     android:layout_marginLeft="16dp" 
     android:layout_marginBottom="4dp" 
     android:layout_gravity="left" /> 
    <!-- This is a centered title --> 
    <TextView 
     android:id="@+id/toolbar_title" 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="?attr/actionBarSize" 
     android:layout_gravity="center" 
     android:gravity="center_vertical" 
     android:visibility="gone" 
     android:text="@string/app_name" 
     android:textColor="@color/colorWhite" 
     style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse" 
     /> 
</FrameLayout> 

這是我的MainActivity:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

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

    final ActionBar ab = getSupportActionBar(); 
    ab.setHomeAsUpIndicator(R.drawable.ic_menu); 
    ab.setDisplayShowHomeEnabled(true); 
    ab.setDisplayHomeAsUpEnabled(true); 
    ab.setDisplayShowCustomEnabled(true); 
    ab.setDisplayShowTitleEnabled(false); 

我怎麼能解決這個問題?

回答

0

未來,當您的應用程序崩潰發佈您的錯誤的堆棧跟蹤,以便我們可以有更多的信息,我們可以幫助您更好地解決您的錯誤。我在這裏看到的這個問題是,你的xml中的T​​oolbar元素是在FrameLayout之外的,記住每個xml文件最多隻能有一個根元素。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto " 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

    <!-- This is a centered logo --> 
    <ImageView 
     android:id="@+id/toolbar_logo" 
     android:src="@drawable/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="?attr/actionBarSize" 
     android:layout_marginTop="4dp" 
     android:layout_marginLeft="16dp" 
     android:layout_marginBottom="4dp" 
     android:layout_gravity="left" /> 
    <!-- This is a centered title --> 
    <TextView 
     android:id="@+id/toolbar_title" 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="?attr/actionBarSize" 
     android:layout_gravity="center" 
     android:gravity="center_vertical" 
     android:visibility="gone" 
     android:text="@string/app_name" 
     android:textColor="@color/colorWhite" 
     style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse" 
     /> 
</FrameLayout> 

你也可能想看看其他ViewGroups安排在你的XML元素,如CoordinatorLayout和LinearLayout中。

+0

沒有工作..... –

+0

@ Mr.Mr我已經更新了我的答案,以便更明確。如果這不起作用,請確保使用錯誤堆棧跟蹤編輯您的答案,以便我和他人在社區中提供幫助。 –

+0

我發現了這個問題。當我刪除「setSupportActionBar(tb);」應用程序正在工作,但操作欄不會更改。 –