Code setting styles for Actionbar and menu in Android

Source: Internet
Author: User

AndroidInActionBarAndMenuHow is the code style set? Today Wheat AcademyAndroidThe development teacher mainly introduces the code setting style of Actionbar and menu in Android, and the friends who have needs can refer to the following

Menu some XML Code

Http://schemas.android.com/apk/res/android ">

<= "div=" ">

android:title= " Find 1 "

android:orderincategory= "100"

android:showasaction= "Always"/>

<= "div=" ">

Android:actionviewclass= "Android.widget.SearchView"

android:showasaction= "Ifroom|collapseactionview"

android:orderincategory= "100"

android:title= " Find 2 "/>

<= "div=" ">

android:title= " shared "

android:orderincategory= "100"

android:icon= "@drawable/ic_action_favor_normal"

android:showasaction= "Never"/>

<= "div=" ">

android:title= " Preservation "

android:orderincategory= "100"

android:showasaction= "Never"/>

<= "div=" ">

android:title= " Font Size "

android:orderincategory= "100"

android:showasaction= "Never"/>

Menu in Overflower menu icon appears complete "Override onmenuopened method, using the principle of reflection "

/**

* Manifest Overflower Menu Icon

*/

@Override

Publicbooleanonmenuopened (Intfeatureid, menu menu) {

if (Featureid = = Window.feature_action_bar && menu!=null) {

if (Menu.getclass (). Getsimplename (). Equals ("Menubuilder")) {

try{

Method m = Menu.getclass (). Getdeclaredmethod ("setoptionaliconsvisible", Boolean.type);

M.setaccessible (TRUE);

M.invoke (menu,true);

}catch (Exception e) {

}

}

}

Returnsuper.onmenuopened (Featureid, menu);

}

for Menu what to do in the menu options

/**

* Menu Menu click Actions to listen to things

*/

@Override

Publicbooleanonoptionsitemselected (MenuItem Item) {

Switch (Item.getitemid ()) {

Caseandroid. R.id.home:

Finish ();

Super.onbackpressed ();

Break

CaseR.id.action_add:

Toast.maketext (This, " add ", Toast.length_short). Show ();

Break

}

returnsuper.onoptionsitemselected (item);

}

Custom ActionBar

/**

* Initialize ActionBar content

* */

Privateactionbar ActionBar;

Privatevoidinitactionbar () {

Actionbar=super.getactionbar ();

Actionbar.show ();

// Manifest Home Area

Actionbar.setdisplayshowhomeenabled (TRUE);

// Set Home Zone fallback button

Actionbar.setdisplayhomeasupenabled (TRUE);

Actionbar.sethomeasupindicator (R.drawable.back_move_details_normal);

// does not appear Home Area title

Actionbar.setdisplayshowtitleenabled (TRUE);//

actionbar.settitle (" news ");// Set title

// does not appear Logo Image

Actionbar.setdisplayuselogoenabled (false);//

// Remove Home area of Icon icon "will icon color set to Bright "

Drawable colordrawable=new

Colordrawable (Android. R.color.transparent);

Actionbar.seticon (colordrawable);

// Custom Area

Actionbar.setdisplayshowcustomenabled (TRUE);

TextView Tvtitle=newtextview (this);//this, at that time the bearer

tvtitle.settext (" News information ");//tvtitle.setid ();

Tvtitle.settextsize (25);

Intcolorval=getresources (). GetColor (R.color.white);

Tvtitle.settextcolor (Colorval);//tvtitle.settextcolor (Color.White);

Tvtitle.setgravity (Gravity.center);

Layoutparams layoutparams=

Newlayoutparams (Layoutparams.match_parent, layoutparams.match_parent);

Actionbar.setcustomview (Tvtitle,layoutparams);

}

Finish the function:

about the Android App to develop a practical tutorial, go to: Http://www.maiziedu.com/course/android/

Code setting styles for Actionbar and menu in Android

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.