Android custom Activity title bar

1. When Activity inherits Activity:

    Hide the title bar:

        Add the following code in the onCreate method:

requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        Add the following attribute value to the Activity configuration information:   android:theme="@android:style/Theme.NoTitleBar"

 

    Custom title bar:

        Add the following code in the onCreate method:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);

        Add the following attribute value to the Activity configuration information: android:theme="@style/TitleTheme"

        Add the following information to the styles.xml file:

<style name="TitleTheme" parent="android:Theme">
                <item name="android:windowTitleSize">50dp</item>
                <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
                <item name="android:windowBackground">@color/white</item>
            </style>

            <style name="WindowTitleBackground">
                <item name="android:background">@color/gray</item>
            </style>

 

 

2. When Activity inherits AppCompatActivity

    Hide the title bar:

         Add the following code in the onCreate method:

requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

         Add the following attribute value to the Activity configuration information: android:theme="@style/Theme.AppCompat.Light.NoActionBar"

 

     Hide title bar 2:

           Add the following code in the onCreate method: getSupportActionBar().hide();

           Add the following attribute value to the Activity configuration information: android:theme="@style/Base.Theme.AppCompat"

 

     Custom title bar:

          Add the following code in the onCreate method:

android.support.v7.app.ActionBar actionBar = getSupportActionBar();
            if(actionBar != null){
                actionBar.setDisplayShowHomeEnabled(false);
                actionBar.setHomeButtonEnabled(false);
                actionBar.setDisplayHomeAsUpEnabled(false);
                actionBar.setDisplayShowCustomEnabled(true);
                actionBar.setDisplayShowTitleEnabled(true);
                View layoutActionbar = LayoutInflater.from(this).inflate(R.layout.layout_titlebar, null);
                actionBar.setCustomView(layoutActionbar);
            }

            Add the following attribute value to the Activity configuration information: android:theme="@style/TitleTheme2"

            Add the following information to the styles.xml file:

<style name="TitleTheme2" parent="Theme.AppCompat">
                        <item name="colorPrimary">@color/gray</item>
                        <item name="android:textColorPrimary">@color/white</item>
                        <item name="android:windowBackground">@color/white</item>
                    </style>

 

3. Create the titlebar file

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout_titlebar"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button_backward"
            android:layout_width="60dp"
            android:layout_height="match_parent"
            android:text="< 返回"
            android:background="@color/gray"
            android:textColor="@color/white"
            android:textSize="14dp" />

        <TextView
            android:id="@+id/text_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal|center"
            android:text="This is a custom title bar"
            android:textColor="@color/white"
            android:textSize="18dp" />
    </RelativeLayout>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326207367&siteId=291194637