PopupWindow可以創建類似對話框風格的窗口,使用PopupWindow創建對話框風格的窗口可以自行定義窗口的位置
public class MainActivity extends Activity {
private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View root = this.getLayoutInflater().inflate(R.layout.labelicon, null);
final PopupWindow pp = new PopupWindow(root, 280, 360);
bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
pp.showAsDropDown(arg0);
pp.showAtLocation(findViewById(R.id.button1), Gravity.CENTER, 20, 20);
}
});
root.findViewById(R.id.imageView5).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pp.dismiss();
}
});
}
}