Android Popupwindow 显示ListView

package com.hyc.popupwindowdemo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	private TextView tvHello = null;
	private PopupWindow mPopupWindow = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tvHello = (TextView) findViewById(R.id.tv_hello);
		tvHello.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TextView tv = new TextView(this);
		// tv.setWidth(LayoutParams.WRAP_CONTENT);
		// tv.setHeight(LayoutParams.WRAP_CONTENT);
		// tv.setText("ok");
		ListView lv = new ListView(this);
		lv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		List<String> strList = new ArrayList<String>();
		strList.add("qwe");
		strList.add("weq");
		strList.add("wer");
		strList.add("rew");
		strList.add("fds");
		strList.add("tre");
		ArrayAdapter<String> strAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strList);
		lv.setAdapter(strAdapter);
		mPopupWindow = new PopupWindow(lv, 200, 400);
		mPopupWindow.setFocusable(true);
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.update();
		mPopupWindow.setBackgroundDrawable(new ColorDrawable(0));
		mPopupWindow.showAsDropDown(tvHello);
	}
}
布局与上一篇一致

你可能感兴趣的:(android,ListView,PopupWindow)