Android Custom window Title sample sharing _android

Source: Internet
Author: User

1, after the construction of the project in its layout folder to create a Title.xml file, as a custom window title file.

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
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, under the Res/drawable file to establish Rectangle.xml file, for the window to apply the gradient effect.

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<shape xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:shape= "Rectangle" >
<!--fill color gradient, do not need the middle color startcolor start and end of the color .-->
<gradient
Android:angle= "270"
Android:endcolor= "#1DC9CD"
Android:startcolor= "#A2E0FB"/>
<!--define inner spacing-->
<padding
android:left= "2DP"
android:top= "2DP"
android:right= "2DP"
android:bottom= "2DP"/>
</shape>

3, Layout file:

Copy Code code as follows:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/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:id= "@+id/button1"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparentleft= "true"
Android:layout_alignparenttop= "true"
android:text= "button"/>
</RelativeLayout>

4, through the activity Background code for custom window settings.

Copy Code code as follows:

Package com.example.customertitle;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.Window;
Import Android.widget.Toast;

Custom title
public class Mainactivity extends activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
1. Set up to use a custom window
Requestwindowfeature (Window.feature_custom_title);
Setcontentview (R.layout.activity_main);
2. XML interface file for introducing custom headers to the window
GetWindow (). Setfeatureint (Window.feature_custom_title, r.layout.title);
}

public void Add (View v) {
Toast.maketext (This, "button is clicked", Toast.length_long). Show ();
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}

5, deployment project, you can display a customized window title. But the custom window caption is a little distance from the left and right sides of the interface and is not completely covered. To solve this problem, you need to overwrite the android window title. The following is the source code for the title of the android window.

Copy Code code as follows:

<!--2. Note: The system window's interface file is android-sdk-windows\platforms\android-8\data\res\layout under the Android system source code SCREEN_CUSTOM_ Title.xml, the contents are as follows:
1. A linear layout-->
<linearlayoutxmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:fitssystemwindows= "true" >
<framelayout android:id= "@android: Id/title_container"
Android:layout_width= "Match_parent"
Android:layout_height= "Android:attr/windowtitlesize"?
style= "Android:attr/windowtitlebackgroundstyle" >
</FrameLayout>
<framelayout android:id= "@android: Id/content"
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 value of the above property is defined in the Themes.xml file under Android-sdk-windows\platforms\android-8\data\res\values:

Copy Code code as follows:

<style name= "Theme" >
<itemname= "Windowcontentoverlay" > @android:d rawable/title_bar_shadow</item>
<itemname= "Windowtitlesize" >25dip</item>
<itemname= "Windowtitlebackgroundstyle" > @android:style/windowtitlebackground</item>
</style>
@android: Style/windowtitlebackground style in android-sdk-windows\platforms\android-8\data\res\ Values in the Styles.xml file in the definition:
<style name= "Windowtitlebackground" >
<itemname= "Android:background" > @android:d rawable/title_bar</item>
</style>


By using the above to know the Android theme style, you now need to inherit the style that overrides it, as follows

Copy Code code as follows:

<resources xmlns:android= "Http://schemas.android.com/apk/res/android" >
<!--define a style that overrides 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 values

Copy Code code 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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.