软件应用中,常看到一种效果是在点击更多时,弹出一更多的弹出框,其效果截图如下:
关键代码如下:
其中MenuView是自定义的一视图,在该视图中,主要是进行图片与文字的布局,以及处理弹出popupwindow弹框
intent = new Intent(this, Test2Activity.class);
spec = mTabHost
.newTabSpec("Test3")
.setIndicator(
new MenuView(this, "更多", res
.getDrawable(R.drawable.pic_more)))
.setContent(intent);
mTabHost.addTab(spec);
mTabHost.setOnTabChangedListener(this);
TabChangeListener事件的处理如下:
@Override
public void onTabChanged(String tabId) {
if(TextUtils.equals(tabId, "Test3")) {
View v = mTabHost.getCurrentTabView();
//设置为空的目的使得事件不再向下传播,即不会响tabchange事件
v.setOnFocusChangeListener(null);
}
}
最后是MenuView的实现:
public class MenuView extends LinearLayout implements OnItemClickListener{
private PopupWindow popuwindWindow;
private ArrayList<HashMap<String, ?>> datas = null;
private Context context;
public MenuView(Context context,String label, Drawable icon) {
super(context);
this.context = context;
datas = new ArrayList<HashMap<String, ?>>();
init$Views();
this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, (float) 1));
setGravity(Gravity.CENTER);
setOrientation(LinearLayout.VERTICAL);
ImageView img = new ImageView(context);
img.setImageDrawable(icon);
addView(img);
TextView tv = new TextView(context);
tv.setText(label);
tv.setGravity(Gravity.CENTER);
tv.setBackgroundColor(Color.TRANSPARENT);
tv.setTextColor(Color.WHITE);
tv.setPadding(0,0, 0, 0);/***dimension***/
tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, (float) 0.0));
addView(tv);
tv.setClickable(false);
tv.setFocusable(false);
tv.setFocusableInTouchMode(false);
img.setClickable(false);
img.setFocusable(false);
img.setFocusableInTouchMode(false);
if(TextUtils.equals("更多", label)) {
setBackgroundDrawable(this.getResources().getDrawable(R.drawable.pub_tab_background_spec_selector));
}
else
setBackgroundDrawable(this.getResources().getDrawable(R.drawable.pub_tab_background_selector));
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//定义弹出时的动画
popuwindWindow.setAnimationStyle(R.style.popupwindow_style);
popuwindWindow.showAsDropDown(this, -popuwindWindow.getWidth(),
this.getTop() + popuwindWindow.getHeight() + 5);
return super.onInterceptTouchEvent(ev);
}
private void init$Views() {
HashMap<String, Object> map = null;
map = new HashMap<String, Object>();
map.put("ico", R.drawable.more_help_img);
map.put("tv", "帮助");
datas.add(map);
map = new HashMap<String, Object>();
map.put("ico", R.drawable.more_netword_img);
map.put("tv", "网络");
datas.add(map);
map = new HashMap<String, Object>();
map.put("ico", R.drawable.more_update_img);
map.put("tv", "更新");
datas.add(map);
map = new HashMap<String, Object>();
map.put("ico", R.drawable.pic_about);
map.put("tv", "关于");
datas.add(map);
LayoutInflater inflater = LayoutInflater.from(context);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.test_gd, null);
GridView gdv = (GridView) layout.findViewById(R.id.test_gv_id);
//进行自适应屏幕的宽度
int tempWidth = 60 * datas.size() + 10 + (datas.size() - 1) * 5;
DisplayMetrics metrics = new DisplayMetrics();
((DashboardActivity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
int disWidth = metrics.widthPixels;
int width = tempWidth > disWidth ? disWidth : tempWidth;
gdv.setLayoutParams(new LayoutParams(width, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
SimpleAdapter simpleAdapter = new SimpleAdapter(context, datas,
R.layout.test_gd_item, new String[] { "ico", "tv" }, new int[] {
R.id.ico_id, R.id.tv_id });
gdv.setAdapter(simpleAdapter);
popuwindWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, true);
ColorDrawable color = new ColorDrawable(0);
popuwindWindow.setBackgroundDrawable(color);
gdv.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
popuwindWindow.dismiss();
}
}