Android开发之自定义View(链式编程)

本文介绍了一种自定义PopupWindow的方法,通过XML布局文件和Java代码实现了一个带有拍照、选取照片及取消等功能的弹出菜单,并详细展示了如何创建、设置及使用这个自定义组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义popupwindow(继承控件)

xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/pop_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/transparent"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:paddingTop="30dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/popwindow_photo"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_take_photo"
                android:layout_width="fill_parent"
                android:layout_height="45dp"
                android:gravity="center"
                android:text="拍照" />

            <View
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:background="#797979" />

            <TextView
                android:id="@+id/tv_pick_photo"
                android:layout_width="fill_parent"
                android:layout_height="45dp"
                android:gravity="center"
                android:text="从相册选择" />
        </LinearLayout>


        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_margin="20dp"
            android:background="@drawable/popwindow_photo"
            android:gravity="center"
            android:text="取消" />
    </LinearLayout>
</RelativeLayout>

新建一个类,继承popupwindow,在其内部添加各种方法用以对其进行相应的操作。

public class SelectPicPopupWindow extends PopupWindow {
    private TextView tv_take_photo, tv_pick_photo, tv_cancel;
    private View mMenuView;

    public SelectPicPopupWindow(Activity context, OnClickListener itemsOnClick) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.popwindow_photo, null);
        tv_take_photo = (TextView) mMenuView.findViewById(R.id.tv_take_photo);
        tv_pick_photo = (TextView) mMenuView.findViewById(R.id.tv_pick_photo);
        tv_cancel = (TextView) mMenuView.findViewById(R.id.tv_cancel);
        //取消按钮  
        tv_cancel.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框  
                dismiss();
            }
        });
        //设置按钮监听  
        tv_pick_photo.setOnClickListener(itemsOnClick);
        tv_take_photo.setOnClickListener(itemsOnClick);
        //设置SelectPicPopupWindowView  
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体可点击  
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果  
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明  
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景  
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框  
        mMenuView.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }

    public SelectPicPopupWindow setPopWindowHight(int height) {
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(height);
        return this;
    }

    public SelectPicPopupWindow setPopWindowWidth(int width) {
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(width);
        return this;
    }
} 
使用时,只需在布局中include引入popupwindow的布局文件。


在Activity中使用步骤如下

                //实例化SelectPicPopupWindow
                menuWindow = new SelectPicPopupWindow(this, itemsOnClick);
//                设置popwindow的宽高
                menuWindow.setPopWindowHight(410).setPopWindowWidth(ActionBar.LayoutParams.MATCH_PARENT);
                //显示窗口
                menuWindow.showAtLocation(this.findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layoutPopupWindow中显示的位置
设置popupwindow的监听

   //为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.tv_take_photo:    
                    break;
                case R.id.tv_pick_photo:   
                    break;
                default:
                    break;
            }
        }
    };

更加深入细致的操作可在SelectPicPopupWindow中去进行添加。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

裴泓博

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值