By default, the title bar (titlebar) of the activity is usually as follows (inside the red box ):
Handlecontacts is the title of the activity.
Sometimes we want to change this monotonous situation. For example, how can I add an icon in the title bar to beautify the interface, add an input box or button, and so on? Let's look at a practical example.
1. Create an android project as follows:
2.drag the image magnifier.png to the res/drawable-mdpi folder of the project. The magniier.png image looks like this:
3. In the Res/layout folder of the project, create a layout titlebar. XML, which will be used to customize the title bar of the activity.
Edit titlebar. XML to make the content as follows:
<?xmlversion="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="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/magnifier"
android:gravity="bottom"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="38dip"
android:text="@string/app_name"
android:textColor="#FFFFFFFF"
android:textSize="14dip"
android:paddingTop="1dip"
/>
<EditText
android:id="@+id/searchparameter"
android:layout_width="wrap_content"
android:layout_height="38dip"
android:text="ABCDEFGHIJ"
android:textSize="14dip"
android:layout_margin="1dip"
/>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="38dip"
android:text="OK"
android:textSize="14dip"
/>
</LinearLayout>
In the preceding linearlayout, the following controls are added:
An imageview used to display an icon
A textview used to display the application name
An edittext for receiving input
A button for testing
4. Modify customizetitlebar. Java to make it as follows:
public class CustomizeTitlebar extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}
}
The above two lines are very important and must appear in the Code strictly in the order above. That is:
Requestwindowfeature (window.Feature_custom_title); Must appear in super. oncreate (savedinstancestate);, setcontentview (R. layout.Main); Before. This means to tell the system that the program should define its own titlebar;
Getwindow (). setfeatureint (window.Feature_custom_title, R. layout.Titlebar); It must appear after setcontentview, which means to tell the system that the custom layout is R. layout. titlebar (that is, the titlebar. xml we wrote earlier)
Here, let's take a look at the results:
We can see that titlebar has basically changed according to our meaning, but there is also a defect: titlebar is too narrow, so how can we change the titlebar height?
5.
To change the titlebar height, we must first create styles. xml:
Edit styles. XML to make the content as follows:
<?xmlversion="1.0" encoding="utf-8"?>
<resources>
<style name="titlebarstyle"parent="android:Theme">
<item name="android:windowTitleSize">38dip</item>
</style>
</resources>
<Item name ="Android: windowtitlesize"> 39dip </item> is used to set the titlebar height.
6. Based on the above, we need to modify the attributes of the corresponding activity in androidmanifest. xml. As follows:
<?xmlversion="1.0" encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.pat.customizetitlebar"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"android:label="@string/app_name">
<activity android:name=".CustomizeTitlebar"
android:label="@string/app_name"
android:theme="@style/titlebarstyle">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8"/>
</manifest>
Note that the bold text is added, and the titlebar is added in step 1. Now let's take a look at the running results:
We can see that the results fully meet our requirements.
7. We can also change the background color of titlebar. To this end, we modify the previous styles. XML to make it as follows:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomizedWindowTitleBackgroundColor">
<item name="android:background">#047BF0</item>
</style>
<style name="titlebarstyle" parent="android:Theme">
<item name="android:windowTitleSize">38dip</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomizedWindowTitleBackgroundColor</item>
</style>
</resources>
Note that the bold characters are newly added.
No changes are required for other project files. The running result is as follows:
8. Finally, we use the OK button as an example to test the Event Response of the control on the titlebar. Therefore, modify customizetitlebar. Java to make it as follows:
public class CustomizeTitlebar extends Activity implements OnClickListener
{
private Button button;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
public voidonClick(View v)
{
if(v.getId() == R.id.button)
{
Toast.makeText(this, "OK button in Titlebar clicked...", Toast.LENGTH_LONG).show();
}
}
}
The bold part is the newly added code. Run the project again. After the interface is displayed, click OK on the titlebar:
This shows that the added controls on titlebar can respond to related events well.
Source: http://blog.csdn.net/pathuang68/article/details/6646792