Maison > Article > base de données > GreenDroid(2)ActionBar的实现
本来想自己写的, 怎么都觉得没有这篇写的好, 就贴个链接了: http://www.samcoles.co.uk/mobile/android-use-greendroid-to-implement-an-actionbar/ This post covers the basics of setting up a project using the GreenDroid library including pullin
本来想自己写的, 怎么都觉得没有这篇写的好, 就贴个链接了:
http://www.samcoles.co.uk/mobile/android-use-greendroid-to-implement-an-actionbar/
This post covers the basics of setting up a project using the GreenDroid library including pulling it from GitHub and how to use the ActionBar it provides. GreenDroid is a useful UI library for Android developed by Cyril Mottier. You can check out all the
features it provides by downloading the GDCatalog app from the Android Market.
The action bar:
is located at the top of the screen to support navigation and highlight important functionalities
replaces the title bar (which is often included into it)
is best used for actions across your app, like search, refresh and compose
can provide a quick link to app home by tapping the app logo
is preferably not contextual, but actions can differ from page to page
Android Patterns
If you don’t have EGit in Eclipse. You’ll need to install it. See here. Begin by opening the “Git Repository Exploring” perspective. Window > Open Perspective. Click the button for “Clone a Git Repository and add the clone to this view” and paste in this to
the URI field: https://github.com/cyrilmottier/GreenDroid.git – the rest of the details should be filled in automatically, so hit next and follow the wizard through. It’s likely you won’t need to change anything.
You’ll notice the GreenDroid repository is now available to you. Open out the branches GreenDroid > Working directory, right click the folder ‘GreenDroid’ and select ‘Import Projects’. Follow the dialog through and click finish. Switch back to the Java Perspective
and you will now have the GreenDroid project imported. You will likely have a problem with the project’s ‘gen’ folder or R.java. If you do (an exclamation mark or cross next to the project name), delete it and then recreate a new folder called gen, if not,
create the folder. I also had to right click the project, Android Tools > Fix Project Properties to get it working as it was targeting a different compiler version to mine.
Next up create your new Android Project, the build target will need to be a minimum of 1.6 to use GreenDroid. Right click on your newly created project, select Properties, then select Android on the left hand side of the dialog. At the bottom of the window
you can click “Add..” and you should have the option to select GreenDroid as a library. Click OK.
Change your default Activity to extend GDActivity. Wherever you want to use the GreenDroid ActionBar your class will need to extend GDActivity, GDListActivity or GDTabActivity. You will also need to remember to no longer call setContentView() to set your layout
and instead use setActionBarContentView(). The former will crash your Activity. Change these and hit ctrl+shift+o to organise your imports.
public class GreenDroidActionBarExampleActivity extends GDActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setActionBarContentView(R.layout.main); } }
public class GDActionBarExampleApplication extends GDApplication { @Override public Class> getHomeActivityClass() { return GreenDroidActionBarExampleActivity.class; } @Override public Intent getMainApplicationIntent() { return new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.app_url))); } }
<string name="app_url">http://www.samcoles.co.uk</string>
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".GDActionBarExampleApplication"></application>
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="Theme.GDActionBarExample" parent="@style/Theme.GreenDroid.NoTitleBar"> <item name="gdActionBarTitleColor">#FFFDD0 <item name="gdActionBarBackground">@drawable/action_bar_background <item name="gdActionBarDividerWidth">2px <item name="gdActionBarApplicationDrawable">@drawable/application_logo </style> </resources>
<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/Theme.GDActionBarExample" android:name=".GDActionBarExampleApplication"></application>Back to the error in themes.xml. It is clear that you could just place an image into your drawable folders called application_logo. But we are going to use a StateDrawable xml so that the logo behaves like a button. Create a folder, /res/drawable and within here create two new xml files called application_logo.xml and action_bar_background.xml, the latter will be a ShapeDrawable for our ActionBar’s background. In application_logo.xml place your state drawable code that defines the image to use for pressed, focused and normal states:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/application_logo_alt"></item> <item android:state_focused="true" android:drawable="@drawable/application_logo_alt"></item> <item android:drawable="@drawable/application_logo_normal"></item> </selector>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#708090"></solid> </shape>
addActionBarItem(Type.Info, ACTION_BAR_INFO);
private static final int ACTION_BAR_INFO = 0;
@Override public boolean onHandleActionBarItemClick(ActionBarItem item, int position) { switch(item.getItemId()) { case ACTION_BAR_INFO: startActivity(new Intent(this, InfoActivity.class)); break; default: return super.onHandleActionBarItemClick(item, position); } return true; }
<activity android:name=".InfoActivity"></activity>
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setActionBarContentView(R.layout.main); setTitle(R.string.info_activity_title); }
<string name="info_activity_title">App Info</string>