android PopupWindow详解

       在 android开发中,我们经常要用到PopupWindow来实现一些弹出框的效果,那什么时候用PopupWindow,什么时候用AlertDialog呢?PopupWindow与AlertDialog的区别在于AlertDialog显示在固定的屏幕中间位置,而popupWindow可以自己设置显示位置。

     popupwindow有两种确定位置的方式:

1.showAsDropDown(View v)在某个组件的下方

2.showAtLocation(View parent, int gravity, int x, int y) 

第一个参数,是相对于哪个view来放置popupwindow

第二个参数,是grivity,和LinearLayout中的grivity一个意思。

最后两个参数是xy方向的偏移量.

下面两个实例可以说明这两种用法的不同。下面我要实现一个居于控件下方的PopupWindow,和一个居于布局底部的PopupWindow。popupWindow的样式如下




activity中的activity_popup_window.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ll_main"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="在组件下方"/>

    <Button
        android:id="@+id/btn_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="在底部位置"/>
</LinearLayout>
 
popupWindow的样式布局popup_test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_delete"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/btn_style"
            android:gravity="center_horizontal"
            android:paddingBottom="20dp"
            android:paddingTop="20dp"
            android:text="Delete"
            android:textColor="@color/redColor"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/btn_style"
            android:gravity="center_horizontal"
            android:paddingBottom="20dp"
            android:paddingTop="20dp"
            android:text="Cancel"
            android:textColor="@color/blueColor"
            android:textSize="16sp"

            />
    </LinearLayout>
</LinearLayout>
按钮样式 btn_style

<?xml version="1.0" encoding="utf-8"?>

<!--画矩形  -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
<!-- 实心填充 -->
    <solid android:color="@color/oneColor" />
<!-- 边角的圆弧半径 -->
   <corners android:radius="5dp" />



</shape>
颜色值
<!--颜色-红色->
<color name="redColor">#e94737</color> 

<!--颜色-蓝色-->
<color name="blueColor">#007aff</color>
activity


public class PopupWindowActivity extends Activity {
    private Button btnPop;
    private Button btnLocation;
    private LinearLayout llMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_window);
        View root = this.getLayoutInflater().inflate(R.layout.popup_test, null);
        final PopupWindow popup = new PopupWindow(root, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        popup.setBackgroundDrawable(new ColorDrawable(-0x90000000));//设置背景半透明
        popup.setFocusable(true);//获得焦点后,按回退键PopupWindow会消失
        btnPop = (Button) findViewById(R.id.btn);
        llMain = (LinearLayout) findViewById(R.id.ll_main);
        btnLocation = (Button) findViewById(R.id.btn_location);
        btnPop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popup.showAsDropDown(v);//在组件下方
            }
        });
        btnLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popup.showAtLocation(llMain, Gravity.BOTTOM, 0, 0);//在布局底部
            }
        });
        root.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {//取消按钮
            @Override
            public void onClick(View v) {
                popup.dismiss();
            }
        });
        root.findViewById(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {//删除按钮
            @Override
            public void onClick(View v) {
                Toast.makeText(PopupWindowActivity.this, "删除", Toast.LENGTH_LONG).show();
            }
        });
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
嗨!对于你关于 Android PopupWindow 的问题,我可以给你一些基本的信息。PopupWindowAndroid 中的一个弹出式窗口,它可以覆盖在其他视图之上,提供一种临时性的用户界面。你可以使用 PopupWindow 来显示额外的内容、菜单或者交互提示。 要创建一个 PopupWindow,首先你需要实例化一个 PopupWindow 对象,并为其设置一些基本属性,例如宽度、高度、背景等。然后,你可以将任何视图或布局添加到 PopupWindow 中,并通过设置位置参数来控制它的显示位置。 下面是一个简单的示例代码,展示如何创建和显示一个 PopupWindow: ```java // 创建一个 PopupWindow 对象 PopupWindow popupWindow = new PopupWindow(context); // 设置 PopupWindow 的宽度和高度 popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); // 设置 PopupWindow 的内容视图 View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null); popupWindow.setContentView(contentView); // 设置 PopupWindow 的背景 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 显示 PopupWindow popupWindow.showAtLocation(anchorView, Gravity.CENTER, 0, 0); ``` 在上面的示例中,我们创建了一个 PopupWindow 对象,并设置了宽度和高度为包裹内容。然后,我们通过调用 `setContentView` 方法将一个自定义的布局文件 `R.layout.popup_layout` 添加到 PopupWindow 中。最后,我们使用 `showAtLocation` 方法将 PopupWindow 显示在屏幕中央。 希望这些信息对你有帮助!如果你对 PopupWindow 有更多的问题,或者需要更详细的示例代码,请随时告诉我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值