5.User Interface/ActionBar

1. ActionBar

  First added in Android 3.0(API level 11)

  

2. Working the Action Bar

  2.1 Removing the action bar

ActionBar actionBar = getSupportActionBar();
actionBar.hide();  

  2.2 Using a logo instead of an icon

    By default, the system uses your application icon in the action bar,as specified by the icon attribute in the <application>or<activity>

      element. However, if you also specify the logo attribute, then the action bar uses the logo image instead of the icon.

3. Adding Action Items

  

  Action bar with three action buttons and the overflow buton

  if the menu items have android:icon and android:title, it be defaluted to show only its icon. android:showAsAction add "withText" property

    to show title and icon together

4.Using spilt action Bar

  Split action bar provides a separate bar at the bottom of the screen to display all action items when the activity is running on a narrow

    screen (such as a portrait-oriented handset).

  

  Mock-ups showing an action bar with tabs(left), then with splite action bar(middle), and with the app icon and title disabled(right)

  To enable split action bar when using the support library, you must do two things:

    <1> Add uiOptions="splitActionBarWhenNarrow" to each <activity> or <application> element

    <2> To support older versions, add a <meta-data> element as a child of each <activity> element that declares the same value

      for "android.support.UI_OPTIONS".

5. Navigating Up with the App Icon

  Enabling the app icon as an Up button allows the user to navigate your app based on the hierarchical relationships between screens.

    For instance, if screen A displays a list of items, and selecting an item leads to screen B, then screen B should include the Up button,

    which returns to screen A.

  To enable the app icon as an Up button, call setDisplayHomeAsUpEnabled(). For example

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    ...
}

  Now the icon in the action bar appears with the Up caret as shown below

  

  To specify the activity to open when the user presses Up button, you have two options:

    <1>Specify the parent activity in the manifest file.

      This is the best option when the parent activity is always the same.

      By declaring in the manifest which activity is the parent, the action bar automatically performs the correct action when the user

        presses the Up button.

<application ... >
    ...
    <!-- The main/home activity (has no parent activity) -->
    <activity
        android:name="com.example.myfirstapp.MainActivity" ...>
        ...
    </activity>
    <!-- A child of the main activity -->
    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <!-- Parent activity meta-data to support API level 7+ -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
</application>

    <2>override getSupportParentActivityIntent() and onCreateSupportNavigateUpTaskStack() in the activity

  

  

6. Adding an Action View

  An action view is a widget that appears in the action bar as a substitute for an action button.

  An action view provides fast access to rich actions without changing activities or fragments, and without replacing the action bar.

  For example, if you have an action for Search, you can add an action view to embeds a SearchView widget in the action bar

android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" //

      

  If you need to configure the action view (such as to add event listeners), you can do so during the onCreateOptionsMenu()

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_activity_actions, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    // Configure the search info and add any event listeners
    ...
    return super.onCreateOptionsMenu(menu);
}

  6.1 Handling collapsible action views

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.options, menu);
    MenuItem menuItem = menu.findItem(R.id.actionItem);
    ...

    // When using the support library, the setOnActionExpandListener() method is
    // static and accepts the MenuItem object as an argument
    MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // Do something when collapsed
            return true;  // Return true to collapse action view
        }

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // Do something when expanded
            return true;  // Return true to expand action view
        }
    });
}

7. Adding an Action Provider

  ....

8. Adding Navigation Tabs

  

  

  http://blog.csdn.net/guolin_blog/article/details/25466665

时间: 2024-10-21 05:52:40

5.User Interface/ActionBar的相关文章

Android UI之Tab(ActionBar+Fragment实现)

Fragment和ActionBar都是Android3.0之后出现的,Fragment,碎片,主要是为了支持更多的UI设计在大屏幕设备上,如平板.因为现在设备的屏幕越来越大,使用Fragment可以更灵活的管理视图层次的变化.像Activity一样,可以创建Fragment来包含View,进行布局,但是Fragment必须嵌入在Activity中,不能单独存在,而且一个Activity可以嵌入多个Fragment,同时一个Fragment可以被多个Activity重用.Action Bar被认

ActionBar 中导航菜单、内容提供中和下拉菜单

一.Action View Action视图是一个在ActionBar上作为Action Button 的替代品.要声明一个视图,需要使用 actionLayout 和 actionViewClass 两个属性中的任意一个来分别定义视图的布局资源和布局类. 下面介绍如何定义 Searview 组件: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://sch

android自定义View之(七)------自定义控件组合仿actionbar控件

我们前面写了6个自定义view的样例,这都是全新自已画的控件.在这个样例中,我们来用几个现有的控件来组合成一个新的控件. 效果图: 我们用二个Button和一个TextView组合来成为一个actionbar,下面先来一个效果图: 关键代码: (1)res/layout/custom_action_bar.xml----组合控件布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android&quo

Android学习路线(二十四)ActionBar Fragment运用最佳实践

通过前面的几篇博客,大家看到了Google是如何解释action bar和fragment以及推荐的用法.俗话说没有demo的博客不是好博客,下面我会介绍一下action bar和fragment在实战中的应用,以及相关demo源码,希望和大家相互交流. 了解过fragment的同学们应该都知道,fragment是android 3.0版本才出现的的,因此如果要在支持android 3.0一下版本的工程中使用fragment的话是需要添加Support Library的.具体如何添加我就不再赘述

Android ActionBar的Overlay模式如何不遮盖顶部内容的问题

关于actionbar的overlay模式请参考 如何让android的actionbar浮动且透明 一文.这篇文章讲的是如何在这种模式下让actionbar不遮住顶部的内容. 这 一般是这样的场景,在一个ListView显示图片的界面中,当ListView向下滑动的时候,actionbar是是浮动在GridView上面一层 的,但是当ListView滚动到顶部,顶部的内容是完全显示出来的,当然这种情况一般ActionBar我们会做成透明效果. 其实很多人都能想到的是,将ListView加上一个

ActionBar 的简单使用

About ActionBar The action bar is one of the most important design elements you can implement for your app's activities. It provides several user interface features that make your app immediately familiar to users by offering consistency between othe

ActionBar的一些理解

1.基本理解 ActionBar是android中的一个控件,从名字可以知道,是一种tab分页式的控件. 当你想要分页的显示的时候,它就派上用场了. 2.另一种理解 我假设我自己要设计一个这样的控件,我会有些什么需求呢? [UI] 我希望每个tab页都有自己的“名字”. 我希望每个tab页都有自己的“界面”. [响应] 我希望每个tab点击的时候,tab页可以发生一些变化,这种变化是我可以自己控制的. 3.标准“解释” [UI] 创建一个ActionBar protected void onCr

Tab 滑动标签,综合ViewPager+Fragment+自定义Tab+ActionBar内容

1.效果图 第二个菜单TAB1,TAB2,TAB3是参照网上的例子,第一个菜单是在它的基础之上改变而来. 2.菜单 这里的菜单是通过两种方式来实现,一种是通过布局文件,一种是通过自定义组件LinearLayout.自定义只需要传入菜单的名字即可,切换时需要监听事件.下面是一个viewpager+fragment实现,在滑动时改变tab的选中项. 自定义tab底部线是采用TranslateAnimation动画来实现滚动,布局文件采用viewpager的方法onPageScrolled和onPag

[Android UI]ActionBar随ScorllView上下拖动而透明度渐变效果

我看到越来越多的应用使用这样的效果,如QQ空间5.0的主界面,确实很好看!大概就搜了一下相关的实现方式,发现早就有了相关的方案: 仿QQ空间滚动ActionBar透明度变化Demo 还有我在github上看到就有这样的实现方式,这也是本博文的主要核心内容: 具体请查看:https://github.com/AChep/Header2ActionBar 效果如下: 这是Demo结构: 1.FadingActionBarHelper.java 这个类是处理Actionbar的核心类,处理对scrol