package cc.wy;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListPopupWindow;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
/**
* Demo描述:
* ListPopupWindow使用完整示例(一)——系统自带ListPopupWindow
*
* 参考资料:
* 1 http://blog.csdn.net/rambomatrix/article/details/23525379
* 2 http://blog.csdn.net/jsnrwzm/article/details/14408835
* Thank you very much
*
* 注意事项:
* 1 ListPopupWindow显示item的布局xml文件的写法
* 2 注意给ListPopupWindow设置anchor.即设置它依附于哪个控件显示
*
*/
public class MainActivity extends Activity {
private Context mContext;
private Button mButton;
private ListPopupWindow mListPopupWindow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mContext=this;
final String itmes[]={"第一个子项","第二个子项","第三个子项"};
mListPopupWindow=new ListPopupWindow(mContext);
mListPopupWindow.setAdapter(new ArrayAdapter<String>(mContext,R.layout.item, itmes));
mListPopupWindow.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
Toast.makeText(mContext, "点击了"+itmes[position], Toast.LENGTH_SHORT).show();
}
});
mButton=(Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//指定anchor
mListPopupWindow.setAnchorView(v);
mListPopupWindow.show();
}
});
}
}
main.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="点击弹出系统ListPopupWindow" />
</RelativeLayout>
item.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>