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你能给我吗请举个例子和解释?谢谢
5 回答
Use ActionBar, see this documentation:
The ShareActionProvider is an extension of ActionProvider that facilitates a “share" action by showing a list of available share targets from the action bar.
With ActionProvider you can add menu and you can create custom your menu item. Here have how to create menu item xml documentation:
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
如果您不需要使用 a ShareActionProvider
,而只需使用Spinner
带有图片和文本的 a ,您可以将子菜单添加到您的操作栏,如下所示:
https ://stackoverflow.com/a/8312985/626787
或者,您可以将 a 添加Spinner
为动作并用CustomAdapter
.
正如@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;
}
}
您的屏幕截图是 aListPopupWindow
使用的 a ShareActionProvider
。