显示下拉编程的动作条/ ActionBarSherlock [英] Show dropdown programatically in ActionBar / ActionBarSherlock

查看:103
本文介绍了显示下拉编程的动作条/ ActionBarSherlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是活动 ActionBarSherlock ActionBar.NAVIGATION_MODE_LIST

当进入我希望在操作栏上的微调,以编程方式扩大后它填充了项目,因此用户需要选择一个项目的页面。截至目前在适配器中的第一项被自动选择。

When entering the page I want the spinner in the action bar to expand programmatically after it's populated with items so the user needs to pick an item. As of now the first item in the adapter is selected automatically.

我想不出一个很好的方式来扩大在操作栏上的微调编程。我需要用一个cutom为了实现这一行为呢?

I can't figure out a nice way to expand the spinner in the action bar programmatically. Do I need to use a cutom view to achieve this behavior?

我看在与 HierarchyViewer 的操作杆和微调不会有一个id集。任何想法?

I've looked on the action bar with the HierarchyViewer and the spinner does not have an id set. Any ideas?

推荐答案

下面是code我如何创建actiobBarSherlock自定义操作栏

Here is code how I create custom action bar with actiobBarSherlock

 private void createCustomActionBar() {

    List<SiteLink> links = new ArrayList<SiteLink>();
    links.add(...)  
    LinksAdapter linkAdapter = new LinksAdapter(this, R.layout.external_link, links);

    View customNav = LayoutInflater.from(this).inflate(R.layout.custom_show_action_bar, null);
    IcsSpinner spinner = (IcsSpinner)customNav.findViewById(R.id.spinner);
    spinner.setAdapter(linkAdapter);


    ImageView refresh = (ImageView) customNav.findViewById(R.id.refresh);
    refresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ...
        }
    });

    ImageView settings = (ImageView) customNav.findViewById(R.id.settings);
    settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ...
        }
    });

    getSupportActionBar().setCustomView(customNav, new ActionBar.LayoutParams(Gravity.RIGHT));
    getSupportActionBar().setDisplayShowCustomEnabled(true);

}

适配器

private static class LinksAdapter extends ArrayAdapter<SiteLink> {

    private List<SiteLink> strings;
    private Context context;

    private LinksAdapter(Context context, int textViewResourceId, List<SiteLink> objects) {
        super(context, textViewResourceId, objects);
        this.strings = objects;
        this.context = context;
    }

    @Override
    public int getCount() {
        if (strings == null) return 0;
        return strings.size();
    }

    @Override
    public SiteLink getItem(int position) {
        return super.getItem(position);
    }


    // return views of drop down items
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {

        final SiteLink siteLink = strings.get(position);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // at 0 position show only icon

        TextView site = (TextView) inflater.inflate(R.layout.external_link, null);
        site.setText(siteLink.getName());

        site.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(siteLink.getUrl()));
                context.startActivity(i);
            }
        });
        return site;


    }


    // return header view of drop down
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return inflater.inflate(R.layout.icon, null);
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
               android:gravity="right"
        >


    <com.actionbarsherlock.internal.widget.IcsSpinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingRight="20dp"
        android:layout_gravity="center"
            />

     <ImageView  android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:src="@drawable/ic_navigation_refresh"
                 android:paddingRight="20dp"
                 android:paddingLeft="10dp"
                 android:layout_gravity="center"
                 android:background="@drawable/action_buttons_background"
                 android:id="@+id/refresh"/>


    <ImageView  android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/ic_action_settings"
                android:paddingRight="20dp"
                android:background="@drawable/action_buttons_background"
                android:layout_gravity="center"
                android:id="@+id/settings"/>

</LinearLayout>

扩大微调通话

to expand spinner call

 (getSupportActionBar().getCustomView().findViewById(R.id.spinner)).performClick();

这篇关于显示下拉编程的动作条/ ActionBarSherlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆