MainActivity:
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import java.util.ArrayList;
/**
* @
* @类名称: ${type_name}
* @类描述: ${todo}
* @创建人: lyp07
* @创建时间:${date} ${time}
* @备注:
*/import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import com.doorcontrol.ruili.my.doorcontrol.R;
import com.doorcontrol.ruili.my.doorcontrol.adapters.MyAdapter;
import java.util.ArrayList;
public class MainActivity extends Activity implements AdapterView.OnItemClickListener, OnClickListener {
private ArrayList list = new ArrayList();
private MyAdapter adapter;
private PopupWindow popupWindow;
private int from = 0;
private Button mPopBottomBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDate();
mPopBottomBtn = (Button) findViewById(R.id.pop_bottom_btn);
mPopBottomBtn.setOnClickListener(this);
}
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
mPopBottomBtn.setText(list.get(position) + "");
popupWindow.dismiss();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.pop_bottom_btn: {
from = Location.BOTTOM.ordinal();
break;
}
}
initPopupWindow();
}
/**
* 添加新笔记时弹出的popWin关闭的事件,主要是为了将背景透明度改回来
*/
class popupDismissListener implements PopupWindow.OnDismissListener {
@Override
public void onDismiss() {
backgroundAlpha(1f);
}
}
protected void initPopupWindow() {
View popupWindowView = getLayoutInflater().inflate(R.layout.pop_list, null);
//内容,高度,宽度
popupWindow = new PopupWindow(popupWindowView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
//菜单背景色
ColorDrawable dw = new ColorDrawable(0xffffffff);
popupWindow.setBackgroundDrawable(dw);
popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main, null),
Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
//设置背景半透明
backgroundAlpha(0.5f);
//关闭事件
popupWindow.setOnDismissListener(new popupDismissListener());
popupWindowView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
ListView listView = (ListView) popupWindowView.findViewById(R.id.listView);
adapter = new MyAdapter(this, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
/**
* 设置添加屏幕的背景透明度 * * @param bgAlpha
*/
public void backgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getWindow().setAttributes(lp);
}
/**
* 菜单弹出方向
*/
public enum Location {
BOTTOM;
}
private void initDate() {
list.add("0");
list.add("1");
list.add("2");
}
}
main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.doorcontrol.ruili.my.textforpopwindow.MainActivity">
item.xml
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_gravity="center"
android:gravity="center"
android:background="#10000000"
android:id="@+id/tv_text"
android:layout_width="match_parent"
android:layout_height="30dp" />
poplist.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.doorcontrol.ruili.my.textforpopwindow.MainActivity">
android:layout_centerHorizontal="true"
android:background="#10000000"
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
adapter:
package com.doorcontrol.ruili.my.textforpopwindow;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* @
* @类名称: ${type_name}
* @类描述: ${todo}
* @创建人:lyp07
* @创建时间:${date} ${time}
* @备注:
*/
public class MyAdapter extends BaseAdapter {
private Context context;
private ArrayList list;
public MyAdapter(Context context,ArrayList list){
this.context=context;
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ViewHolder vh;
if (convertView == null) {
vh = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
vh.textview = (TextView) convertView.findViewById(R.id.tv_text);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.textview.setText(list.get(position) + "");
return convertView;
}
static class ViewHolder {
TextView textview;
}
}
差不多也就这样了