Heim Datenbank MySQL-Tutorial GreenDroid(2)ActionBar的实现

GreenDroid(2)ActionBar的实现

Jun 07, 2016 pm 03:26 PM
实现 Eigen

本来想自己写的, 怎么都觉得没有这篇写的好, 就贴个链接了: 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

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 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);
    }
}
Nach dem Login kopieren

Next add a new class to your project that extends GDApplication. Override the getHomeActivityClass() method and the getMainApplicationIntent() methods. The former will need to return your main home/launcher activity and the latter will return an Intent to view the relevant website for your app:

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)));
    }
 
}
Nach dem Login kopieren


You will also need to add the the app_url string to your res/values/strings.xml file:


<string name="app_url">http://www.samcoles.co.uk</string>
Nach dem Login kopieren

Next update your AndroidManifext.xml to use your new Application class:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".GDActionBarExampleApplication"></application>
Nach dem Login kopieren

Right click your res/values folder and create a new xml values file, call it themes.xml. In here we will specify some app-wide styles for your ActionBar. Ignore the error on the gdActionBarApplicationDrawable and gdActionBarBackground value for now:

<?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>
Nach dem Login kopieren

Again, update the application tag in your AndroidManifest.xml to use this theme:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/Theme.GDActionBarExample" android:name=".GDActionBarExampleApplication"></application>
Nach dem Login kopieren
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>
Nach dem Login kopieren

and in action_bar_background.xml the ShapeDrawable code. This is just a solid block of colour, but you could specify a gradient or even use an image:

<?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>
Nach dem Login kopieren

Next you will need to place the images you’ve specified in the application_logo.xml StateDrawable into your project. The GDCatalog app uses the dimensions of 205×51 pixels for the hdpi versions and 135×34 for the mdpi version. I haven’t yet needed to experiment with different sizes so mine are the same dimensions, and these suit the ActionBar proportions well. Place these into the relevant res/drawable-hdpi and res/drawable-mdpi folders.

You should now be able to run the project and test it out! It won’t look much but you can tap the logo and it should open the URL specified in your app_url string. Notice also that it changes to your application_logo_alt.png image when touched or selected.

Next we will make the ActionBar a little more useful by adding a button to it that will take us to our application’s info activity. Back in GreenDroidActionBarExampleActivity, simply add an info button by placing this call in your onCreate() method:

addActionBarItem(Type.Info, ACTION_BAR_INFO);
Nach dem Login kopieren

Also add that constant to the top of your class:

private static final int ACTION_BAR_INFO = 0;
Nach dem Login kopieren

Go ahead and run it again. The info button is in! But you’ll notice that nothing happens when you click it.

To enable this button to do something we need to override onHandleActionBarItemClick(). Right click your Activity, source > Override/Implement methods and choose it from the options under GDActivity. You can get the value of the id that was passed in the second parameter of addActionBarItem  by calling getItemId() on the item. So create a switch block on this value and start InfoActivity (we’ll create this next) if the info button has been pressed:

@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;
}
Nach dem Login kopieren

Create the new class InfoActivity that extends GDActivity, and don’t forget to add it to your manifest.

<activity android:name=".InfoActivity"></activity>
Nach dem Login kopieren

In your onCreate() method of InfoActivity set the title to be displayed in the Action Bar:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setActionBarContentView(R.layout.main);
    setTitle(R.string.info_activity_title);
}
Nach dem Login kopieren

Remember to add this string to your strings.xml file also:

<string name="info_activity_title">App Info</string>
Nach dem Login kopieren

Now run your app! Notice that the InfoActivity Action Bar has a home button and a title in place of the application logo. Tap the home button to return to the home activity you just came from.

Working Android 1.6 source for this post is available on github. To checkout the other features of GreenDroid you can also import the project for the GDCatalog app from the GreenDroid github repository.
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
1 Monate vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
1 Monate vor By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Crossplay haben?
1 Monate vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Wie implementiert man die doppelte WeChat-Anmeldung auf Huawei-Mobiltelefonen? Wie implementiert man die doppelte WeChat-Anmeldung auf Huawei-Mobiltelefonen? Mar 24, 2024 am 11:27 AM

Wie implementiert man die doppelte WeChat-Anmeldung auf Huawei-Mobiltelefonen? Mit dem Aufkommen der sozialen Medien ist WeChat zu einem unverzichtbaren Kommunikationsmittel im täglichen Leben der Menschen geworden. Viele Menschen können jedoch auf ein Problem stoßen: Sie können sich gleichzeitig auf demselben Mobiltelefon bei mehreren WeChat-Konten anmelden. Für Huawei-Mobiltelefonbenutzer ist es nicht schwierig, eine doppelte WeChat-Anmeldung zu erreichen. In diesem Artikel wird erläutert, wie eine doppelte WeChat-Anmeldung auf Huawei-Mobiltelefonen erreicht wird. Erstens bietet das EMUI-System, das mit Huawei-Mobiltelefonen geliefert wird, eine sehr praktische Funktion – das doppelte Öffnen von Anwendungen. Durch die doppelte Öffnungsfunktion der Anwendung können Benutzer gleichzeitig

Verwenden Sie Java, um Code zur Implementierung von Liebesanimationen zu schreiben Verwenden Sie Java, um Code zur Implementierung von Liebesanimationen zu schreiben Dec 23, 2023 pm 12:09 PM

Liebesanimationseffekte durch Java-Code realisieren Im Bereich der Programmierung sind Animationseffekte sehr verbreitet und beliebt. Mit Java-Code können verschiedene Animationseffekte erzielt werden, darunter der Herzanimationseffekt. In diesem Artikel wird erläutert, wie Sie mithilfe von Java-Code diesen Effekt erzielen, und es werden spezifische Codebeispiele aufgeführt. Der Schlüssel zum Realisieren des Herzanimationseffekts besteht darin, das herzförmige Muster zu zeichnen und den Animationseffekt durch Ändern der Position und Farbe der Herzform zu erzielen. Hier ist der Code für ein einfaches Beispiel: importjavax.swing.

PHP-Programmierhandbuch: Methoden zur Implementierung der Fibonacci-Folge PHP-Programmierhandbuch: Methoden zur Implementierung der Fibonacci-Folge Mar 20, 2024 pm 04:54 PM

Die Programmiersprache PHP ist ein leistungsstarkes Werkzeug für die Webentwicklung, das eine Vielzahl unterschiedlicher Programmierlogiken und Algorithmen unterstützen kann. Unter diesen ist die Implementierung der Fibonacci-Folge ein häufiges und klassisches Programmierproblem. In diesem Artikel stellen wir vor, wie Sie die Fibonacci-Folge mit der Programmiersprache PHP implementieren, und fügen spezifische Codebeispiele bei. Die Fibonacci-Folge ist eine mathematische Folge, die wie folgt definiert ist: Das erste und das zweite Element der Folge sind 1, und ab dem dritten Element ist der Wert jedes Elements gleich der Summe der beiden vorherigen Elemente. Die ersten paar Elemente der Sequenz

So implementieren Sie die WeChat-Klonfunktion auf Huawei-Mobiltelefonen So implementieren Sie die WeChat-Klonfunktion auf Huawei-Mobiltelefonen Mar 24, 2024 pm 06:03 PM

So implementieren Sie die WeChat-Klonfunktion auf Huawei-Mobiltelefonen Mit der Popularität sozialer Software und der zunehmenden Bedeutung von Datenschutz und Sicherheit rückt die WeChat-Klonfunktion allmählich in den Mittelpunkt der Aufmerksamkeit der Menschen. Die WeChat-Klonfunktion kann Benutzern helfen, sich gleichzeitig bei mehreren WeChat-Konten auf demselben Mobiltelefon anzumelden, was die Verwaltung und Nutzung erleichtert. Es ist nicht schwierig, die WeChat-Klonfunktion auf Huawei-Mobiltelefonen zu implementieren. Sie müssen lediglich die folgenden Schritte ausführen. Schritt 1: Stellen Sie sicher, dass die Version Ihres Mobiltelefonsystems und die WeChat-Version den Anforderungen entsprechen. Stellen Sie zunächst sicher, dass die Version Ihres Huawei-Mobiltelefonsystems sowie die WeChat-App auf die neueste Version aktualisiert wurden.

Entwicklungsvorschläge: So verwenden Sie das ThinkPHP-Framework zur Implementierung asynchroner Aufgaben Entwicklungsvorschläge: So verwenden Sie das ThinkPHP-Framework zur Implementierung asynchroner Aufgaben Nov 22, 2023 pm 12:01 PM

„Entwicklungsvorschläge: So verwenden Sie das ThinkPHP-Framework zur Implementierung asynchroner Aufgaben“ Mit der rasanten Entwicklung der Internettechnologie stellen Webanwendungen immer höhere Anforderungen an die Verarbeitung einer großen Anzahl gleichzeitiger Anforderungen und komplexer Geschäftslogik. Um die Systemleistung und das Benutzererlebnis zu verbessern, erwägen Entwickler häufig die Verwendung asynchroner Aufgaben, um einige zeitaufwändige Vorgänge auszuführen, z. B. das Senden von E-Mails, das Verarbeiten von Datei-Uploads, das Erstellen von Berichten usw. Im Bereich PHP bietet das ThinkPHP-Framework als beliebtes Entwicklungsframework einige praktische Möglichkeiten zur Implementierung asynchroner Aufgaben.

Meistern Sie, wie Golang Möglichkeiten für die Spieleentwicklung eröffnet Meistern Sie, wie Golang Möglichkeiten für die Spieleentwicklung eröffnet Mar 16, 2024 pm 12:57 PM

Im heutigen Bereich der Softwareentwicklung wird Golang (Go-Sprache) als effiziente, prägnante und hochgradig parallele Programmiersprache von Entwicklern zunehmend bevorzugt. Seine umfangreiche Standardbibliothek und die effizienten Parallelitätsfunktionen machen es zu einer hochkarätigen Wahl im Bereich der Spieleentwicklung. In diesem Artikel wird untersucht, wie man Golang für die Spieleentwicklung verwendet, und seine leistungsstarken Möglichkeiten anhand spezifischer Codebeispiele demonstriert. 1. Golangs Vorteile bei der Spieleentwicklung: Als statisch typisierte Sprache wird Golang beim Aufbau großer Spielsysteme verwendet.

Implementierungshandbuch für PHP-Spielanforderungen Implementierungshandbuch für PHP-Spielanforderungen Mar 11, 2024 am 08:45 AM

Implementierungsleitfaden für PHP-Spielanforderungen Mit der Popularität und Entwicklung des Internets erfreut sich der Markt für Webspiele immer größerer Beliebtheit. Viele Entwickler hoffen, die PHP-Sprache zur Entwicklung ihrer eigenen Webspiele nutzen zu können, und die Umsetzung der Spielanforderungen ist ein wichtiger Schritt. In diesem Artikel wird erläutert, wie Sie mithilfe der PHP-Sprache allgemeine Spielanforderungen implementieren und spezifische Codebeispiele bereitstellen. 1. Spielfiguren erstellen In Webspielen sind Spielfiguren ein sehr wichtiges Element. Wir müssen die Attribute des Spielcharakters wie Name, Level, Erfahrungswert usw. definieren und Methoden für deren Bedienung bereitstellen

So implementieren Sie eine exakte Divisionsoperation in Golang So implementieren Sie eine exakte Divisionsoperation in Golang Feb 20, 2024 pm 10:51 PM

Die Implementierung exakter Divisionsoperationen in Golang ist ein häufiger Bedarf, insbesondere in Szenarien mit Finanzberechnungen oder anderen Szenarien, die hochpräzise Berechnungen erfordern. Der in Golang integrierte Divisionsoperator „/“ wird für Gleitkommazahlen berechnet, und manchmal besteht das Problem eines Präzisionsverlusts. Um dieses Problem zu lösen, können wir Bibliotheken von Drittanbietern oder benutzerdefinierte Funktionen verwenden, um exakte Divisionsoperationen zu implementieren. Ein gängiger Ansatz ist die Verwendung des Rat-Typs aus dem Paket math/big, der eine Darstellung von Brüchen bereitstellt und zur Implementierung exakter Divisionsoperationen verwendet werden kann.

See all articles