Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Android custom title bar     - CentOS 6.6 install rsync server (Server)

- Experience RHEL7 new features (Linux)

- Save the database data files into Oracle Learning (Database)

- MongoDB start under Linux (Database)

- How to use Android Studio development / debugging Android source code (Linux)

- Free compiler install MySQL-5.6.14 (Database)

- Linux operation and maintenance of the actual file system, file links (Linux)

- Yii2 Advanced Version Copy New Project Problem Solved (Programming)

- Use Ambari rapid deployment Hadoop big data environment (Server)

- MySQL and MariaDB traditional master-slave cluster configuration (Database)

- Oracle Incident Packaging Service (Database)

- Easily solve the MySQL database connection error too many (Database)

- Seven Steps to Help Google Chrome Speed - (Linux)

- Oracle inverted reverse function (Database)

- YUM install desktop environment in CentOS (Linux)

- Ubuntu prompt / lack of boot space solutions (Linux)

- Oracle Execute to Parse perform analytical Ratio Analysis (Database)

- Unity Greeter Badges: the lost session icon back to the login screen Ubuntu (Linux)

- Linux environment installation of rvm and ruby (Linux)

- Iptables principle (Linux)

  Android custom title bar
  Add Date : 2018-11-21      
  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,
// 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 {
    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.
- Systemd on RHEL7 (Linux)
- Linux crontab commands and detailed usage examples (Linux)
- C ++ CBitmap, HBitmap, Bitmap difference and contact (Programming)
- findmnt- Looking mounted file system (Linux)
- Introduction to thread pooling and simple implementation (Programming)
- Android child thread really do not update UI (Programming)
- Use C program in JavaScript (Programming)
- Add your own kernel and ramfs based on an existing Linux LiveCD (Linux)
- Linux use logs to troubleshoot (Linux)
- Java memory area and memory overflow exception (Programming)
- The difference between Linux su and sudo commands (Linux)
- C # / iOS / Android Universal Encryption and decryption (Programming)
- Oracle how to assess the true concurrent session (Database)
- Oracle to start to solve the error ORA-27102 (Database)
- Python Django direct implementation of sql statement (Programming)
- Linux (CentOS) directory file management and file system file compression packing (Linux)
- Linux find and xargs (Linux)
- Linux5.8 installed phpMyAdmin was unable to issue related php-mcrypt (Database)
- Ubuntu install VMware Workstation 11 tutorials at 14.04 / 14.10 (Linux)
- RabbitMQ tutorial examples: RabbitMQ installation under Windows (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.