Android PopupWindows

来源:互联网 发布:网络认证 编辑:程序博客网 时间:2024/05/01 03:38

今天了解到PopupWindows这个布局,PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。

下面是一个实例

xml文件

<LinearLayout
        android:id="@+id/ll_popup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#ffffff"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#ff495a" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/item_popupwindows_camera"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:background="@drawable/bt_nobgd"
                android:textColor="#585858"
                android:textSize="18sp"
                android:text="拍照" />
        </LinearLayout>

        <TextView
            android:layout_marginLeft="80dp"
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="#f2f2f2" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/item_popupwindows_Photo"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:background="@drawable/bt_nobgd"
                android:textColor="#585858"
                android:textSize="18sp"
                android:text="从相册中选取" />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#f3f3f3" />

        <Button
            android:id="@+id/item_popupwindows_cancel"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:background="@drawable/bt_nobgd"
            android:textColor="#585858"
            android:textSize="18sp"
            android:text="取消" />
    </LinearLayout>

MainActivity文件   用来实现拍照获取本地图库的功能。

package cn.pb.activity;


import java.io.File;

import com.example.pb_android.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

    private TextView t_send,t_ragion;
    private Button btn_images;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(R.layout.activity_main);
        Init();
    }
    public void Init(){
        t_send = (TextView) findViewById(R.id.t_send);
        btn_images = (Button) findViewById(R.id.btn_images);
        t_ragion = (TextView) findViewById(R.id.t_ragion);
        
        t_send.setOnClickListener(this);
        btn_images.setOnClickListener(this);
        t_ragion.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.t_send:
            break;
        case R.id.btn_images:
            new PopupWindows(MainActivity.this, v);
            break;
        case R.id.t_ragion:
            break;
        default:
            break;
        }
    }
    
    public class PopupWindows extends PopupWindow {

        public PopupWindows(Context mContext, View parent) {

            View view = View
                    .inflate(mContext, R.layout.item_popupwindows, null);
            view.startAnimation(AnimationUtils.loadAnimation(mContext,
                    R.anim.fade_ins));
            LinearLayout ll_popup = (LinearLayout) view
                    .findViewById(R.id.ll_popup);
            ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext,
                    R.anim.push_bottom_in_2));

            setWidth(LayoutParams.FILL_PARENT);
            setHeight(LayoutParams.FILL_PARENT);
            setBackgroundDrawable(new BitmapDrawable());
            setFocusable(true);
            setOutsideTouchable(true);
            setContentView(view);
            showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            update();
            Button bt1 = (Button) view
                    .findViewById(R.id.item_popupwindows_camera);
            Button bt2 = (Button) view
                    .findViewById(R.id.item_popupwindows_Photo);
            Button bt3 = (Button) view
                    .findViewById(R.id.item_popupwindows_cancel);
            bt1.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(
                            MediaStore.ACTION_IMAGE_CAPTURE);
                    // 下面这句指定调用相机拍照后的照片存储的路径
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
                            .fromFile(new File(Environment
                                    .getExternalStorageDirectory(),
                                    "imageTemp.jpg")));
                    startActivityForResult(intent, 2);
                    dismiss();     //关闭Pop窗口
                }
            });
            bt2.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_PICK, null);
                    intent.setDataAndType(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            "image/*");// 调用android的图库
                    startActivityForResult(intent, 1);
                    dismiss();
                }
            });
            bt3.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    }

}


0 0
原创粉丝点击