Android custom title, android title
We know that the system provides a black title for every Activity we create by default. In this article, I will show you how to customize the title style. Compared with the style provided by the system, the custom title can meet our ideal custom design, making our interface look more advanced, so as to better attract users to use. The following introduces today's content:
1. Since it is a custom title style, first we need to design a custom title layout. With this layout file, we can design our title style (title. xml) as we like ):
<? 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 = "wrap_content" android: textColor = "# aa0000" android: text = "this is my custom title"/> <Button android: id = "@ + id/button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "more"/> </LinearLayout>
2. After writing the layout file, Let's design the title style. styles. xml under the res directory of the project:
<Resources> <style name = "itcastTheme" parent = "android: Theme"> <item name = "android: windowContentOverlay "> @ color/nonecolor </item> <item name =" android: windowTitleSize "> 44dp </item> <! -- Set the width of the custom title --> <item name = "android: windowTitleBackgroundStyle"> @ style/itcastbg </item> <! -- Customize the title style --> </style> <style name = "itcastbg"> <item name = "android: background "> @ drawable/rectangle </item> </style> </resources>
3. The red font section is a title background implemented by the rectangle. xml file under the drawable file:
<?xml version="1.0" encoding="utf-8"?><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>
4. Here I can start modifying our main Activity:
Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_CUSTOM_TITLE); setContentView (R. layout. activity_main); getWindow (). setFeatureInt (Window. FEATURE_CUSTOM_TITLE, R. layout. title); // set our custom title Button mybutton = (Button) findViewById (R. id. button); mybutton. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Toast. makeText (MainActivity. this, "Please look forward to it", Toast. LENGTH_SHORT ). show ();}});}}
Note that the red part must be written before the layout file is referenced, otherwise it will not be effective.
5. Finally, we need to set the style for our Activity in the AndroidManifest. xml file:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.edu.hpu.activity_title" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name"> <activity android:name=".MainActivity" android:theme="@style/itcastTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
All right, the introduction to the custom title of Android is complete, and you can implement it if you are interested. Learn from new users and communicate with experts.