Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305
- 主题:用户界面之PopupWindow(弹出窗口)
-
PopupWindow和AlertDialog的区别:
AlertDialog是非阻塞式的:AlertDialog弹出时,后台还可以做事情。
PopupWindow是阻塞式的:在PopupWindow退出前,只允许我们操作PopupWindow,其他操作被阻塞。
PopupWindow(实例):
给PopupWindow定义一个布局。
popupwindow.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#336699"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myimage" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1_sure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"/>
<Button android:id="@+id/button2_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
/>
</LinearLayout>
</LinearLayout>
MainActivity.java:
import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
initPopWindow();
}
});
}
private void initPopWindow() {
// 加载PopupWindow的布局文件
View contentView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.popupwindow, null);
// 声明Popupwindow弹出窗口
PopupWindow popupWindow = new PopupWindow(null,100,300);
popupWindow.setContentView(contentView);
//获得焦点
popupWindow.setFocusable(true);
/**
* popupWindow的三个显示方法:
* showAsDropDown(View view)弹出对话框,位置在紧挨着view组件
showAsDropDown(View anchor, int x, int y)弹出对话框,位置在紧挨着view组件,x y代表偏移量
showAtLocation(View parent, int gravity, int x, int y)弹出对话框,位置相对于父布局的位置,x y代表偏移量
*/
popupWindow.showAsDropDown(button);
ImageView imageview = (ImageView) contentView.findViewById(R.id.imageview);
Button button_sure = (Button) contentView.findViewById(R.id.button1_sure);
Button button_cancel = (Button) contentView.findViewById(R.id.button2_cancel);
}
}
运行结果:
Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305
时间: 2024-11-14 10:19:05