Add the Action Bar

AndroidActionBar

  1. API版本在3.0(11)或更高,需要使用Theme.Holo创建actionbar。
  2. API版本需要兼容2.1(7),需要继承ActionBarActivity,并设置该activity的Theme为Theme.Appcompat.Light。
  3. API版本需要兼容2.1,ActionButton中的showAsAction属性的namespace为android会无效,建议namespace为appname。
  4. 关于actionbar的上部导航添加返回图标的事例代码如下:
  5. 设置返回按钮显示时,3.0以下api是getSupportActionBar.setDisplayHomeAsUpEnabled,3.0以上就是getAction.setDisplayHomeAsUpEnabled.
  6. 学习链接:http://wear.techbrood.com/training/basics/actionbar/index.html
  7. <application ... >
        ...
        <!-- The main/home activity (it 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 4.0 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfirstapp.MainActivity" />
        </activity>
    </application>

SupportActionBar(转载)

1.设置加载条和加载圈

复制代码
 1     @Override
 2     protected void onCreate(Bundle savedInstanceState) {
 3         // Bar和下面的Activity背景都为白色
 4         setTheme(R.style.Theme_Sherlock_Light);
 5         //圆形加载必须在setContextView之前调用
 6         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
 7         //条形加载必须在setContextView之前调用
 8         requestWindowFeature(Window.FEATURE_PROGRESS);
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_main);
11         //启用圆形加载
12         setSupportProgressBarIndeterminateVisibility(false);
13         //启用条形加载
14         setSupportProgressBarVisibility(true);setSupportProgress(进度);
15     }
复制代码

2.是否显示添加的View

1         /**
2          * 添加view,然后设置是否显示
3          */
4         getSupportActionBar().setCustomView(R.layout.custom_view);
5         getSupportActionBar().setDisplayShowCustomEnabled(false);

3.属性设置

复制代码
1   /**
2    * 显示ActionBar
3    */
4    getSupportActionBar().show();
5  /**
6   * 隐藏ActionBar
7   */
8    getSupportActionBar().hide();
复制代码

4.Navigation设置

1                         /**
2                          * 显示标准的ActionBar
3                          */
4                         getSupportActionBar().setNavigationMode(
5                                 ActionBar.NAVIGATION_MODE_STANDARD);
复制代码
1                        /**
2                          * 显示有list的ActionBar
3                          */
4                         getSupportActionBar().setNavigationMode(
5                                 ActionBar.NAVIGATION_MODE_LIST);
复制代码
 1         /**为NAVIGATION_MODE_LIST添加list
 2          * R.array.locations文字数组
 3          * R.layout.sherlock_spinner_item默认就OK
 4          * R.layout.sherlock_spinner_dropdown_item默认就OK
 5          */
 6         Context context = getSupportActionBar().getThemedContext();
 7         ArrayAdapter<CharSequence> listAdapter = ArrayAdapter
 8                 .createFromResource(context, R.array.locations,
 9                         R.layout.sherlock_spinner_item);
10         listAdapter
11                 .setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
       getSupportActionBar().setListNavigationCallbacks(listAdapter, ActionBar.OnNavigationListener);

其中的ActionBar.OnNavigationListener接口用于监听点击事件
复制代码
复制代码
复制代码
                        /**
                         * 显示有tab的ActionBar
                         */
                        getSupportActionBar().setNavigationMode(
                                ActionBar.NAVIGATION_MODE_TABS);
复制代码
 1         /**
 2          * 添加tabitem
 3          */
 4         ActionBar.Tab newTab = getSupportActionBar().newTab();
 5         //为item添加子布局
 6         //newTab.setCustomView(R.layout.tab_custom_view);
 7         //为item添加icon
 8         //newTab.setIcon(R.drawable.ic_title_share_default);
 9         //为item添加标题
10         newTab.setText("Text!");
11         //为item添加监听
12         newTab.setTabListener(实现这个接口ActionBar.TabListener);
13         //添加到Actionb中
14         getSupportActionBar().addTab(newTab);
复制代码
复制代码
1                         /**
2                          * 隐藏/显示Tittle
3                          */
4                         getSupportActionBar().setDisplayShowTitleEnabled(true);
1                         /**
2                          * 隐藏/显示Custom
3                          */
4                         getSupportActionBar().setDisplayShowCustomEnabled(true);
1                         /**
2                          * 添加子标题
3                          */
4                         getSupportActionBar().setSubtitle(
5                                 "The quick brown fox jumps over the lazy dog.");
1                         /**
2                          * 隐藏/显示返回箭头
3                          */
4                         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
1                         /**
2                          * 隐藏/显示功能图片
3                          */
4                         getSupportActionBar().setDisplayShowHomeEnabled(true);
1                         /**
2                          * 设置使用activity的logo还是activity的icon
3                          * true为logo
4                          */
5                         getSupportActionBar().setDisplayUseLogoEnabled(true);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值