问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

如何实现ActionBar标题栏处的点击事件

发布网友 发布时间:2022-04-14 22:07

我来回答

2个回答

懂视网 时间:2022-04-15 02:28

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

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)));
 }
 
}


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


http://www.samcoles.co.uk

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


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:



 

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

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:



 
 
 
 
 
 
 

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:


 

 
 
 

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);

Also add that constant to the top of your class:

private static final int ACTION_BAR_INFO = 0;

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;
}

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


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);
}

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

App Info

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.

热心网友 时间:2022-04-14 23:36

  我敢肯定你对这个标题肯定心存疑惑,但事实就是如此,这个标题完全适合 Android 开发人员。据我所知, Android 程序员不情愿写 findViewById ()、点击事件监听等重复率较高的代码。那我们如何才能缩短编写业务逻辑代码的时间,并且避免写那些重复的代码呢?所以让我们来成为一个能偷懒又有高效率的 Android 程序员。想知道怎么做吗?不用急,接下来我就会写到。  有许多第三方的库和框架是可供我们使用。出于种种原因,我们并不知道这些库或者知道但还没用过。有的开发者开发了自己定义的库或者并不想使用第三方的库。如果我们在应用程序开发的过程中使用一些第三方库,也许可以提高程序的可兼容性、漂亮的 UI 界面、让代码变得整洁等等。所以,我将研究更多像这样的第三方库来帮助初学者和有经验的开发人员。  今天,让我们来讨论下“依赖注入函数库”。  什么是依赖注入?  依赖注入是一种软件设计模式,无论是在运行时还是在编译时,允许删除、改变硬编码依赖性。[来自 Wikipedia](维基百科资源):   少量的代码让 Android 开发人员省力同时,也让他们能更专注于实际的业务逻辑。  RoboGuice 和 ActionBarSherlock  正如我前面提到的,你得在 RoboActivity 和 RoboFragment 中继承其中一个才能在 Activity 事件或 Fragment 中使用 RoboGuice。但是如果你已经在项目中使用了 ActionBarSherlock 去编译呢?那问题就在于,你已经继承了 SherlockActivity 或 SherlockFragmentActivity 中的一个。现在问题是,你不能同时使用 RoboGuice 和 ActionBarSherlock。  解决方法是,为 Activities 和 Fragments 定义一个基类。然后你就能同时使用 RoboGuice 和 ActionBarSherlock 了。
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
...低压90一110间、高压140一160之间吃什心药好 形体舞四大要素 广樱热水器价格是多少 广樱电热水器质量怎么样 广樱热水器品牌定位是什么,主要面向那些用户群体 广樱燃气热水器质量怎么样 广樱热水器售后服务如何 华伦天奴有哪些款式的高跟鞋比较受欢迎? 华伦天奴rockstud 系列鞋子的特点是什么? 门为什么用铆钉 佳能有没有带全景扫描功能的数码相机 yy各个时段的人气频道和人气节目分别是什么,怎么查询各个时段人气最高的节目 老婆总是睡觉前让我添她的妹妹爱才满足睡下,这是什么问题? 一级建造师会不会在发布成绩时卡通过率? 我老婆阴道的味道是苦的,是什么原因引起 伊斯兰教法规定丈夫亲妻子的下面有罪吗 老婆给我添,代表她爱我吗? 一级建造师考试是每年划定通过率,还是只要及格就可以了? 请问一级建造师的考试是按通过率卡还是及格为准? 一级建造师考试是按及格率,还是按单科成绩及格怎么评判 一级建造师的分数是你考试的卷面分数吗?还是按通过率从高分往低分录,通过率以外的统一都是不级格啊? 在京东上买了台电视没安装现在商家不退怎样处理? 在京东商城买了一个海信42寸电视,和一个挂架,打了海信客服热线,说是安装要收费。问一下,要收多少钱? 在京东买的电视10900发生质量问题,且这个电视已经停产没有换货的可能 京东商城买的东西不能送到怎么办? 在京东买了个电视架!结果不能用,京东又不给退怎么办?谢谢了! 老当益壮!56岁阿汤哥的《碟中谍6》为什么那么好看? 电影碟中谍6:全面瓦解剧情 《碟中谍6》拍摄时阿汤哥受伤又起身继续拍,为何50多岁了他还这么拼命? 碟中谍6具体剧情? 如何同步接聊天记录? 如何同步接聊天记录? 碟中谍6的剧情简介 碟中谍的介绍 如何在Fragment中使用Actionbar-Android开发问答 《碟中谍6》主要演员片酬大概是多少,这样的片酬合理吗? 怎么晒冬瓜干 如何通过actionbar和tab跳到不同的activity 《纯真的年代》全集在哪里看 求纯真的年代高清百度云资源 韩剧请回答1997 1998等等有什么联系吗 需要按顺序看吗 还是独立的 请问,(纯真的年代)电视的主题曲是那首歌吗 《人世间》这部电视剧,为什么一经播出,就是王炸呢? 薏米仁好像煮不烂怎样办? 煮薏米不用提前用水泡,只需多加一步,几分钟快速煮烂开花,实用 煮薏米不用提前泡,教你几个小方法,快速煮烂,省时又省火 薏米怎样快速快速煮烂而且不便气味不变味道 那里买饮用水表里面的电池,谢谢 如何同步接聊天记录? 卡布西游噬魂秘技怎么打?玩家心得