How to make a custom tite bar without changing the

2019-08-11 07:32发布

问题:

I dont like how the CustomTheme looks and I want to keep the AppTheme.

but when I use a customized title bar with AppTheme it crashes telling me I cant combine numerous styles.

How can I do that?

Thanks!

回答1:

First crate your own custom layout for the ActionBar

tab_header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/header_title"
    android:textSize="18dp"
    android:layout_gravity="center"
    android:singleLine="true"
    android:maxLines="1"
    android:paddingLeft="@dimen/dim_10"
    android:paddingRight="@dimen/dim_10"
    android:textAllCaps="false"
    android:textColor="@color/white"
    />
   </LinearLayout>

Then in your Activity call (With Home Button Enabled)

    getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
    getActionBar().setCustomView(R.layout.tab_header);

    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);

Without Home Button

    getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getActionBar().setCustomView(R.layout.tab_header);

    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);