My Android advanced tour ------ & gt; Android custom window title instance

Source: Internet
Author: User

With the extension of this instance, you can make menu navigation and other practical functions in the Custom title,

1. provide an interface for custom titles

2. Apply the custom title to the Activity window.

3. Change the default theme set for Activity in android to your own theme.

========================================= ======================================

Step 2: Write the layout file/res/layout/custom_title.xml for the custom title

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "horizontal" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: background = "@ drawable/rectangle"> <! -- Specify the background, which you have drawn. --> <Button android: id = "@ + id/infoAtMeTextView" android: textColor = "# FF0000" android: layout_width = "wrap_content" android: layout_height = "match_parent" android: layout_weight = "1" android: gravity = "center" android: text = "\ @ me"/> <Button android: id = "@ + id/infoCommentTextView" android: textColor = "# FF0000" android: layout_width = "wrap_content" android: layout_height = "match_parent" android: layout_weight = "1" android: gravity = "center" android: text = "comment"/> <Button android: id = "@ + id/infoPrivateMsgTextView" android: textColor = "# FF0000" android: layout_width = "wrap_content" android: layout_height = "match_parent" android: layout_weight = "1" android: gravity = "center" android: text = "private message"/> </LinearLayout>

Step 3: the background used in the above layout file is a drawable file, which draws a rectangle. The file is/drawable/rectangle. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <! -- The following defines a rectangle --> <shape xmlns: android = "http://schemas.android.com/apk/res/android" android: shape = "rectangle"> <gradient android: angle = "270" android: endColor = "# 1DC9CD" android: startColor = "# A2E0FB"/> <padding android: left = "2dp" android: top = "2dp" android: right = "2dp" android: bottom = "2dp"/> </shape>

Step 4. Set the custom title to Activity, CustomTitleActivity. java

Package cn. oyp. title; import android. app. activity; import android. OS. bundle; import android. view. window; public class CustomTitleActivity extends Activity {/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // specify the use of the custom title requestWindowFeature (Window. FEATURE_CUSTOM_TITLE); setContentView (R. layout. main); // set the custom title layout file getWindow () of the window (). setFeatureInt (Window. FEATURE_CUSTOM_TITLE, R. layout. custom_title );}}


Then run the application and find that the entire title bar cannot be filled in the custom layout after the User-Defined layout is set.

By viewing the Android source code, it is found that the Android system sets a layout file for the title of the Activity by default. The layout file is core/res/layout/screen_title.xml.

<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2006 The Android Open Source Project     Licensed under the Apache License, Version 2.0 (the "License");     you may not use this file except in compliance with the License.     You may obtain a copy of the License at            http://www.apache.org/licenses/LICENSE-2.0       Unless required by applicable law or agreed to in writing, software     distributed under the License is distributed on an "AS IS" BASIS,     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.     See the License for the specific language governing permissions and     limitations under the License.--><!--This is an optimized layout for a screen, with the minimum set of featuresenabled.--><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:fitsSystemWindows="true">    <FrameLayout        android:layout_width="match_parent"         android:layout_height="?android:attr/windowTitleSize"        style="?android:attr/windowTitleBackgroundStyle">        <TextView android:id="@android:id/title"             style="?android:attr/windowTitleStyle"            android:background="@null"            android:fadingEdge="horizontal"            android:gravity="center_vertical"            android:layout_width="match_parent"            android:layout_height="match_parent" />    </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>

After reading a piece of code, we can find that the layout file consists of two frames, and several attributes of the layout File

Title height

Title background Style

? Android: attr/windowContentOverlay title foreground color

The values of these attributes are assigned values in the core/res/values/themes. xml file.

<style name="Theme"><item name="android:windowContentOverlay">@android:drawable/title_bar_shadow</item><item name="android:windowTitleSize">25dp</item><item name="android:windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item></style>

The above @ android: style/WindowTitleBackground style is defined in the core/res/values/styles. xml file.

        <style name="WindowTitleBackground"><item name="android:background">@android:drawable/title_bar</item></style>


Step 5: Customize the style/res/values/style. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <! -- This style inherits the default style of the system --> <style name = "customTheme" parent = "android: Theme"> <! -- Set the title foreground to transparent --> <item name = "android: windowContentOverlay"> @ drawable/nocolor </item> <! -- Set the title height to 44dp --> <item name = "android: windowTitleSize"> 44dp </item> <! -- Set the title background color --> <item name = "android: windowTitleBackgroundStyle"> @ style/customBg </item> </style> <! -- Define a background style --> <style name = "customBg"> <item name = "android: background "> @ drawable/rectangle </item> </style> </resources>

The above @ drawable/nocolor is defined in the/res/values/strings. xml file.

<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> the application is designed to customize the window title! </String> <string name = "app_name"> Custom window title </string> <! -- Define a transparent color --> <drawable name = "nocolor" >#00000000 </drawable> </resources>

<? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "cn. oyp. title "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 8 "/> <application android: icon = "@ drawable/icon" android: label = "@ string/app_name"> <activity android: name = ". customTitleActivity "android: theme =" @ style/customTheme "> <! -- Use a custom topic --> <intent-filter> <action android: name = "android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> </application> </manifest>

Step 7: view the effect of the custom window

< p>

< strong>

< strong>

< p>

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.