|
Android APP developers often use custom title bar, and the case of multiple-level pages also need to pass the data to customize the title bar.
This article points:
Incomplete filling custom title
Custom title bar back button click event
A code
Here to tell us about the process:
1. Create a title bar layout file mytitlebar.xml
2. Create mytitlestyle topic in the style.xml
3. Create a class CustomTitleBar
4. In the need to customize the title bar of the Activity of OnCreate method instantiates CustomTitleBar
5. Use a custom title bar Activity defined in AndroidManifest.xml topic
1. Define a custom title bar layout mytitlebar.xml
< ? Xml version = "1.0" encoding = "utf-8"?>
< RelativeLayout
android: id = "@ + id / re_title" xmlns: android = "http://schemas.android.com/apk/res/android"
android: layout_width = "fill_parent"
android: layout_height = height "50dp" // define a custom title bar
android: background = "@ color / start_background"
android: orientation = "horizontal">
< ImageButton
android: scaleType = "fitXY"
android: layout_alignParentLeft = "true"
android: layout_centerVertical = "true"
android: layout_marginLeft = "10dp"
android: id = "@ + id / bt_back"
android: layout_width = "25dp"
android: layout_height = "25dp"
android: src = "@ drawable / left_back"
android: background = "@ color / touming" />
< TextView
android: id = "@ + id / mytitle"
android: layout_centerInParent = "true"
android: layout_width = "wrap_content"
android: layout_height = "match_parent"
android: gravity = "center" // make text in the middle of the entire title bar
android: textColor = "# fff"
android: textSize = "20dp" />
< / RelativeLayout>
2. Create mytitlestyle topic in the style.xml
< Resources>
< - Custom title bar parent =! "Android: Theme" This property must be written ->
< Style name = "mytitlestyle" parent = "android: Theme">
< ! - Set the height, and the consistent mytitlebar.xml ->
< Item name = "android: windowTitleSize"> 50dp < / item>
< ! - Within the setting filled with zeros to make custom title fills the entire title bar, otherwise there is a gap on both sides ->
< Item name = "android: padding"> 0dp < / item>
< / Style>
< / Resources>
3. Create a class CustomTitleBar
public class CustomTitleBar {
private Activity mActivity;
// Do not use static pages will complain because there are three return
/ **
* @param Activity
* @param Title
* @see [Custom title bar]
* /
public void getTitleBar (Activity activity, String title) {
mActivity = activity;
activity.requestWindowFeature (Window.FEATURE_CUSTOM_TITLE);
// Specify a custom layout file defines the title bar
activity.setContentView (R.layout.mytitlebar);
activity.getWindow (). setFeatureInt (Window.FEATURE_CUSTOM_TITLE,
R.layout.mytitlebar);
// Get custom title bar TextView control and set the contents of the passed string
TextView textView = (TextView) activity.findViewById (R.id.mytitle);
textView.setText (title);
// Set the back button click event
ImageButton titleBackBtn = (ImageButton) activity.findViewById (R.id.bt_back);
titleBackBtn.setOnClickListener (new OnClickListener () {
public void onClick (View v) {
// Call system return button click event
mActivity.onBackPressed ();
}
});
}
}
4. In the need to customize the title bar of the Activity of OnCreate method instantiates CustomTitleBar, here is the food page
public class food extends Activity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
// Instantiate CustomTitleBar pass the appropriate parameters
CustomTitleBar ct = new CustomTitleBar ();
ct.getTitleBar (this, "food");
setContentView (R.layout.page_food);
}
}
5. Use a custom title bar Activity defined in AndroidManifest.xml topic
// Omitted the rest, android: theme = "@ style / mytitlestyle" necessary to write the phrase
< Activity
android: name = ". food"
android: label = "@ string / activity_food"
android: theme = "@ style / mytitlestyle" />
Second, the summary
Use custom title bar, many people will encounter filled with dissatisfaction, both sides there is a gap and the return button click event does not respond, where tests and summarizes the most appropriate manner.
Custom title bar fills dissatisfaction, there are many online solutions, some more complicated, I am here directly when you define a property Theme solved, but also relatively easy to understand.
Custom title bar the back button click event does not respond or wrong issue, but also a lot of code to test online with onBackPressed () is most convenient, but also someone uses finish (), like the rest of the test OnKeyDown not pass. |
|
|
|