SoFunction
Updated on 2025-04-10

Android custom window title example sharing

1. After building the project, create a file in its layout folder as a file for the custom window title.

Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="@string/hello_world"
        android:textColor="#FF00FF"
         />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="add"
android:text="Add" />
</LinearLayout>

2. Create a file under the res/drawable file to apply a gradient effect to the window.

Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:andro
    android:shape="rectangle" >
<!--  The fill color is a gradient color, and the intermediate color startColor start and end color is not required.->
    <gradient
        android:angle="270"     
        android:endColor="#1DC9CD"
        android:startColor="#A2E0FB"/>
<!-- Define the inner spacing -->
    <padding
        android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
</shape>

3. Layout file:

Copy the codeThe code is as follows:

<RelativeLayout xmlns:andro
    xmlns:tools="/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <Button
        android:
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />
</RelativeLayout>

4. Customize window settings through activity background code.

Copy the codeThe code is as follows:

package ;

import ;
import ;
import ;
import ;
import ;
import ;

//Custom title
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
// 1. Set the custom window
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(.activity_main);
// 2. Introduce a custom title xml interface file to the window
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, );
    }

    public void add(View v) {
(this, "Button is clicked", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;
    }
}

5. Deploy the project and display the customized window title. However, the custom window title is a little distance from the left and right ends of the interface and is not completely covered. To solve this problem, it is necessary to override the window title of android. Below is the source code of the Android window title.

Copy the codeThe code is as follows:

<!--2. Note: The interface file of the system window is screen_custom_title.xml under the Android system source code android-sdk-windows\platforms\android-8\data\res\layout, and the content is as follows:
1. A linear layout-->
 <LinearLayoutxmlns:andro
    android:orientation="vertical"
    android:fitsSystemWindows="true">
    <FrameLayout android:
        android:layout_width="match_parent"
        android:layout_height="?android:attr/windowTitleSize"
        style="?android:attr/windowTitleBackgroundStyle">
    </FrameLayout>
    <FrameLayout android:
        android:layout_width="match_parent"
        android:layout_height="0dip"
         android:layout_weight="1"
        android:foregroundGravity="fill_horizontal|top"
        android:foreground="?android:attr/windowContentOverlay" />
 </LinearLayout>

android:attr/windowTitleSize
android:attr/windowTitleBackgroundStyle
android:attr/windowContentOverlay

The values ​​of the above attributes are defined in the file under android-sdk-windows\platforms\android-8\data\res\values:

Copy the codeThe code is as follows:

   <style name="Theme">
       <itemname="windowContentOverlay">@android:drawable/title_bar_shadow</item>
        <itemname="windowTitleSize">25dip</item>
       <itemname="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
   </style>
@android:style/WindowTitleBackground style is defined in the file under android-sdk-windows\platforms\android-8\data\res\values:
   <style name="WindowTitleBackground">
        <itemname="android:background">@android:drawable/title_bar</item>
   </style>


Through the above, we can know the theme style of Android. Now we need to inherit and rewrite its style. The code is as follows

Copy the codeThe code is as follows:

<resources xmlns:andro>
<!-- Define a style to cover the original theme style -->
    <style name="myTheme" parent="android:Theme">
        <item name="android:windowContentOverlay">@drawable/color</item>
        <item name="android:windowTitleSize">50dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/textViewBg</item>
    </style>

    <style name="textViewBg">
        <item name="android:background">@drawable/rectangle</item>
    </style>
</resources>

Definition of color value

Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">CustomerTitle</string>
    <string name="action_settings">Settings</string>
<string name="hello_world">Custom title</string>
    <drawable name="color">#00000000</drawable>
</resources>