0

I am using a dropdown menu and i put inside it only a strings but i want when i click on the action bar on the drop down menu widget that when the list of choices drops i will see not only strings but pictures for each choice , i试图查找它,但我没有找到与如何做相关的东西。我正在谈论的示例是这个 http://2.bp.blogspot.com/-mVQddiSMz0I/TzL2y2Ny5uI/AAAAAAAABKo/ikNPtWiPu4w/s1600/Screenshot_2012-02-08-14-01-21.png你能给我吗请举个例子和解释?谢谢

4

5 回答 5

0

Use ActionBar, see this documentation:

Adding an Action Provider

The ShareActionProvider is an extension of ActionProvider that facilitates a “share" action by showing a list of available share targets from the action bar.

enter image description here

With ActionProvider you can add menu and you can create custom your menu item. Here have how to create menu item xml documentation:

Menu Resource

And see example: menu with photo

And here have how to add icon stream to inten for ShareIntent see: ShareActionProvider with one icon - looking as simple actionitem

于 2013-04-19T11:40:32.657 回答
0

如果您不需要使用 a ShareActionProvider,而只需使用Spinner带有图片和文本的 a ,您可以将子菜单添加到您的操作栏,如下所示: https ://stackoverflow.com/a/8312985/626787

或者,您可以将 a 添加Spinner为动作并用CustomAdapter.

于 2013-04-19T11:50:57.143 回答
0

正如@mbarrben 所说,您不需要ShareActionProvider. 您只需要一个 Spinner 并实现一个 CustomAdapter。下面你可以看到一个简单的例子(完整的例子在这里

在您的活动中,您应该拥有:

public class SpinnerIconActivity extends SherlockActivity implements OnNavigationListener{

private SpinnerAdapter mSpinnerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner_icon);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);


    List<SpinnerRow> options = new ArrayList<SpinnerRow>();
    options.add(new SpinnerRow("Droid 1", R.drawable.android01));
    options.add(new SpinnerRow("Droid 2", R.drawable.android02));
    options.add(new SpinnerRow("Droid 3", R.drawable.android03));
    mSpinnerAdapter  = new MySpinnerAdapter(getApplicationContext(),
            R.layout.spinner_row, R.id.text, options);

        actionBar.setListNavigationCallbacks(mSpinnerAdapter, this);
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
   }

   @Override
   public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    return false;
   }

}

最重要的是编写您的自定义适配器。我选择使用 ArrayAdapter 因为使用它我只需要覆盖一种方法

编写 customAdapter 的方法与创建可定制的 ListView 相同,但有一个基本区别,而不是重写 getView 方法,您必须重写 getDropDownView

所以,下面是我的例子:

public class MySpinnerAdapter extends ArrayAdapter<SpinnerRow> {

private List<SpinnerRow> rows;
private int resource;

public MySpinnerAdapter(Context context, int resource,
        int textViewResourceId, List<SpinnerRow> objects) {
    super(context, resource, textViewResourceId, objects);
    rows = objects;
    this.resource = resource;
}

static class ViewHolder {

    TextView text;
    ImageView icon;
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    ViewHolder  holder = new ViewHolder();
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(resource,
                parent, false);

        holder.text = (TextView) convertView.findViewById(R.id.text);
        holder.icon = (ImageView) convertView.findViewById(R.id.icon);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    SpinnerRow currentRow = rows.get(position);
    holder.text.setText(currentRow.toString());
    holder.icon.setImageDrawable(
            getContext().getResources().getDrawable(currentRow.getIconResourceId()));


    return convertView;
}

}
于 2013-04-19T13:25:14.513 回答
0

您的屏幕截图是 aListPopupWindow使用的 a ShareActionProvider

于 2013-04-19T11:45:17.683 回答
0

请参阅 google => http://developer.android.com/guide/topics/ui/actionbar.html#ActionProvider提供的链接

于 2013-04-19T13:43:44.413 回答