您可以在单击按钮时显示 AwardDialog 而非上下文菜单, 以选择行厚度 。
给例如。
public void lineButtonClick(View v) {
Builder builder = new Builder(this);
builder.setTitle("Line Thickness");
builder.setAdapter(new LineAdapter(this), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// here you get index of clicked item in "which" parameter
}
});
builder.create().show();
}
class LineAdapter extends BaseAdapter {
// these are the line images with different thickness
int lineIds[] = {
R.drawable.line1, R.drawable.line2, R.drawable.line3, R.drawable.line4, R.drawable.line5
};
@Override
public int getCount() {
return lineIds.length;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.raw_line, null);
holder = new ViewHolder();
holder.imgLine = (ImageView)convertView.findViewById(R.id.imgLine);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imgLine.setImageResource(lineIds[position]);
return convertView;
}
class ViewHolder {
ImageView imgLine;
}
}
// raw_line.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>