Actionbarutil: Quickly add titles

Source: Internet
Author: User

Actionbarutil Tool class used to quickly add titles to the activity that inherits Actionbaractivity


Tool class:

public class Actionbarutil {private static final String TAG = "Actionbarutil"; /** * Initialize Actionbar * * @param ctx Context * @param title Actionbar title Resource ID */public static void SE    Tup (Context ctx, int title) {Setup (CTX, ctx.getstring (title), true); }/** * Initialize Actionbar * * @param ctx Context * @param title Actionbar title */public static void S    Etup (Context ctx, String title) {Setup (CTX, title, true); }/** * Initialize Actionbar * * @param ctx Context * @param title Actionbar title Resource ID * @par Am ishomeenabled Enable return key */public static void Setup (Context ctx, int title, Boolean ishomeenabled) {if (Ti        Tle <-1) {LOG.E (TAG, "title resource Error");        } else if (title = = 0) {Setup (CTX, "", ishomeenabled);        } else {setup (CTX, ctx.getstring (title), ishomeenabled); }}/** * Initialize Actionbar * * @param ctx ConText * @param title Actionbar * @param ishomeenabled whether the return key is enabled */public static void Setup (Context CTX, String title, Boolean ishomeenabled) {if (ctx instanceof actionbaractivity) {ActionBar ActionBar            = ((actionbaractivity) ctx). Getsupportactionbar ();            if (Textutils.isempty (title)) {actionbar.setdisplayshowtitleenabled (false);            } else {Actionbar.settitle (title);            } actionbar.setdisplayhomeasupenabled (ishomeenabled);        Actionbar.sethomebuttonenabled (ishomeenabled);        } else {log.e (TAG, "initialization Actionbar error, title is empty, or the current context is not an instance of actionbaractivity"); }    }}


Use:

public class Babaactivity extends Actionbaractivity {    @Override    protected void OnCreate (Bundle Savedinstancestate) {        super.oncreate (savedinstancestate);        Actionbarutil.setup (This, "Personal homepage");    }    @Override Public    Boolean onoptionsitemselected (MenuItem Item) {//This method can be written in baseactivity        if (Item.getitemid ( ) = = Android. R.id.home) {            finish ();        }        return super.onoptionsitemselected (item);}    }



Actionbarutil: Quickly add titles

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.