Farewell Actionbarsherlock--android.support bag can also be built 2.x compatible Actionbar (1)

Source: Internet
Author: User

Http://www.bdqn.cn/news/201308/10625.shtml

Abstract: Farewell Actionbarsherlock--android.support package can also be built to 2.x compatible Actionbar (1)

Actionbar is the product of Android 3.0 that has been extended to the newest 4.3 now

Personally, Actionbar is more concise than traditional pop-up menus.

But Actionbar only API 10 or more (including API 10) to taste

Then some of the great gods created the Actionbarsherlock, bringing a compatible Actionbar library to the 2.x.

In this year's Google I/O, Google is finally planning to put the official 2.x compatible Actionbar library for everyone to use.

Now I'll share some of the methods used.

You can update the Android support library via SDK Manager to Ver18 to get this library

After the update can be found in the <SDK directory >/extras/android/support/v7 AppCompat folder This is compatible with 2.x Actionbar Library

or manual download: http://pan.baidu.com/share/link?shareid=3793697689&uk=1527727709

After importing the library

Need to change the theme in Styles.xml (be sure to remember to switch otherwise FC when starting)

[HTML]

1

2

<!--This is a black theme--

<style name= "Appbasetheme" parent= "Theme.appcompat" >

[HTML]

1

2

<!--This is a white theme--

<style name= "Appbasetheme" parent= "Theme.AppCompat.Light" >

[HTML]

1

2

<!--This is a white theme with black Actionbar---

<style name= "Appbasetheme" parent= "Theme.AppCompat.Light.DarkActionBar" >

Here is the code example (Note that the public class Xxxactivity extends actionbaractivity, not the extends Activity):

[Java]

Package com.example.testabc;

Import Android.os.Bundle;

Import Android.support.v4.view.MenuItemCompat;

Import android.support.v7.app.ActionBarActivity;

Import Android.view.Menu;

Import Android.view.MenuItem;

Import Android.widget.Toast;

public class Mainactivity extends Actionbaractivity {

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

}

@Override

public boolean Oncreateoptionsmenu (Menu menu) {

Menuitemcompat.setshowasaction (

Menu

. Add ("the")

. SetIcon (Android. R.drawable.ic_menu_rotate),

Menuitemcompat.show_as_action_if_room);

Menuitemcompat.setshowasaction (

Menu

. Add ("No.2")

. SetIcon (Android. R.drawable.ic_menu_compass),

Menuitemcompat.show_as_action_if_room);

Menuitemcompat.setshowasaction (

Menu

. Add ("No.3")

. SetIcon (Android. R.drawable.ic_menu_more),

Menuitemcompat.show_as_action_if_room);

return true;

}

@Override

public boolean onoptionsitemselected (MenuItem menu) {

if (menu.gettitle () = = "The winner") {

Toast.maketext (Getapplicationcontext (), "you clicked First button.", Toast.length_short). Show ();

}

if (menu.gettitle () = = "No.2") {

Toast.maketext (Getapplicationcontext (), "you clicked Second button.", Toast.length_short). Show ();

}

if (menu.gettitle () = = "No.3") {

Toast.maketext (Getapplicationcontext (), "you clicked Third button.", Toast.length_short). Show ();

}

return super.onoptionsitemselected (menu);

}

}

Put

PS: A little puzzled is that the Theme.Compat.Light white theme on the 2.x and 4.x display effect is different, but the other two themes show is normal.

Maybe a friend asked me why I have only two buttons on the 2.x, because my screen is too small. The third button is divided into the pop-up menu, to be found by pressing the menu key.

Farewell Actionbarsherlock--android.support bag can also be built 2.x compatible Actionbar (1)

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.