android中popwindow的使用
jopen
9年前
android开发中popwindow的使用,首先需要定义popwindow的布局,这里定义的比较简单
pop_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#ff0000" > <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" /> <Button android:id="@+id/cancle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout>
在MainActivity中获得自定义的popWindow布局,实例化popwindow对象,然后定义其显示位置,显示动画等
MainActivity.java
package com.example.popwindow; import android.os.Bundle; import android.app.Activity; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.PopupWindow; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button open = (Button) findViewById(R.id.open); open.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_item,null); Button ok = (Button) view.findViewById(R.id.ok); Button cancle = (Button) view.findViewById(R.id.cancle); final PopupWindow popWindow = new PopupWindow(view, 100,100); popWindow.setAnimationStyle(R.anim.pop); popWindow.showAsDropDown(arg0); ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"you click ok",1000).show(); } }); cancle.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub popWindow.dismiss(); } }); } }); } }
pop.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0" android:toAlpha="1" android:duration="2000"> </alpha> </set>
来自: http://blog.csdn.net//mockingbirds/article/details/40260437