2016-07-18 5 views
1

私はカスタムアクションバーを使用したいので、私は自分のアプリケーションにカスタムアクションバーを設定します。 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のツールバー要素がFrameLayoutの外側にあることを確認しています。各xmlファイルに最大で1つのルート要素を含めることができます。

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

また、このようなCoordinatorLayoutとのLinearLayoutなど、あなたのXMLの要素を、配置する他のViewGroupsで見たいと思うかもしれません。

+0

が働いていません..... –

+0

@ Mr.Mr私は私の答えをより明示的に更新しました。これがうまくいかない場合は、コミュニティ内の自分や他の人が助けることができるように、エラースタックトレースで回答を編集してください。 –

+0

問題が見つかりました。私が "setSupportActionBar(tb);を削除すると、アプリは動作していますが、アクションバーは変更されません。 –

関連する問題