Android ActionBar backbutton and tabs

可紊 提交于 2020-01-01 08:57:15

问题


I want to create an ActionBar and a tabbed navigation like the google+ app.

I used this example as a starting point and now I have a great actionbar:

  • http://developer.android.com/resources/samples/ActionBarCompat/index.html

I've also included a ViewPager and a TabHost to have tabs and scrolling left/right Fragments.

What i need is to show the back arrow in version prior to honeycomb. If I set getActionBar().setDisplayHomeAsUpEnabled(true), the arrow is automatically show in version >= honeycomb.

  • How can I do that in version prior to honey?

What I also want to have is Tabs like the google+ app.

This is how is my tab bar looks:

removed dead ImageShack link

...and this is what i want:

removed dead ImageShack link

I can't find any example to style tab bars like that one.


回答1:


I would recommend you the ActionBarSherlock for ActionBar compatibility with Android <3. It is a better implementation that the example offered in the developers page.

Using this package, the back arrow will appear when you add to your activity the following line.

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Regarding the Google+ tabs, I have found the widget ViewFlow very useful and looking exactly the same. Moreover, you don't have any backward compatibility issues. Have a look to the CircleFlowIndicator example.




回答2:


You can use ActionBarSherlock and a ViewPager with ViewPagerIndicator to achieve the desired look. These libraries also work pre ICS.

As miguel.rodelas suggested, use getSupportActionBar().setDisplayHomeAsUpEnabled(true) for the home arrow. In onOptionsItemSelected you can catch it by the id android.R.id.home.




回答3:


ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);

//use your custom xml view here

View actionBarView = getLayoutInflater().inflate(R.layout.action_bar_custom_view, null);
        actionBar.setCustomView(actionBarView);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

//your logic for click listner

setListenerForActionBarCustomView(actionBarView);



回答4:


As stated, download the ActionBarSherlock and import it as a library here https://api.github.com/repos/JakeWharton/ActionBarSherlock/zipball/4.2.0

  • Instead of using getActionBar use getSupportActionBar
  • Download the support library
  • Instead of Activity, extend your class as SherlockActivity
  • Same with fragments, use SherlockFragment

Then just copy your code, should work perfectly.



来源:https://stackoverflow.com/questions/8518316/android-actionbar-backbutton-and-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!