ActionBar的下拉了列框

ActionBar中的下拉菜单操作

  在ActionBar中添加下拉菜单,主要有一下几个关键步骤:

  1. 生成一个SpinnerAdapter,设置ActionBar的下拉菜单的菜单项

  2. 实现ActionBar.OnNavigationListener接口,当点击ActionBar的菜单项是进行相应的操作

  3. 调用setNavigationMode()方法将ActionBar的操作模型设置为ActionBar.NAVIGATION_MODE_LIST. 注意:这个步骤应该在Activity的onCreate()回调函数时执行

  4. 调用setListNavigationCallbacks()方法为ActionBar添加适配器和监听器.

   

  以下是例子代码,这个例子中只是简单测试,ArrayAdapter是SpinnerAdapter的间接子类,这里ActionBar中的菜单项是一个数组的数据,当点击菜单的其中一项,就会生成一个对应的Fragment代替原来的Fragment.Fragment里面也只有简单的一个TextView控件,用来显示选择的信息。

  自定义的Fragment:

复制代码
public class StudentInfo extends Fragment
{
    // Fragment对应的标签,当Fragment依附于Activity时得到
    private String tag;

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        tag = getTag();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        TextView textView = new TextView(getActivity());
        textView.setText(tag);
        return textView;
    }
}
复制代码

   在strings.xml中定义一个数组,这个数组就是菜单项的信息:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ActionBar下拉菜单</string>

    <string-array name="student">
        <item>邓名侨</item>
        <item>刘天喜</item>
        <item>其黄石</item>
        <item>蔡宏远</item>
    </string-array>

</resources>
复制代码

 

  主要的程序片段,MainActivity:

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

 

   程序运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值