美文网首页
PopupWindow的简单使用(从底部弹出和回收)

PopupWindow的简单使用(从底部弹出和回收)

作者: 沈溺_16e5 | 来源:发表于2019-03-25 08:29 被阅读0次
1、Activity 对应的 xml 布局里
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:id="@+id/ll">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="popupwindow" />

</LinearLayout>
2、PopupWindow的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/c_ffffff"
        android:orientation="vertical">

        <TextView
            android:id="@+id/photograph"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="拍照"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/tv1"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:background="#000000" />

        <TextView
            android:id="@+id/photo_album"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="相册"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:background="#C2C2C2" />

        <TextView
            android:id="@+id/cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="取消"
            android:textSize="30sp" />

    </LinearLayout>

</RelativeLayout>
3、在res文件夹下,新建anim文件夹,新建两个文件:
// anim_enter
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="500"/>
  <alpha android:fromAlpha="0" android:toAlpha="1.0" android:duration="500"/>
 </set>
// anim_exit
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="500"/>
  <alpha android:fromAlpha="1.0" android:toAlpha="0" android:duration="500"/>
</set>
4、在values中的style文件中添加样式:
<style name="PopAnimation">
  <!-- Customize your theme here. -->
  <item name="android:windowExitAnimation">@anim/anim_exit</item>
  <item name="android:windowEnterAnimation">@anim/anim_enter</item>
</style>
5、Activity
View head_portrait_popupwindow_item = LayoutInflater.from(PersonalDetailsActivity.this).inflate(R.layout.head_portrait_popupwindow_item, null);
final PopupWindow popupWindow = new PopupWindow(head_portrait_popupwindow_item, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

// popupWindow 弹出后,界面设置为灰色
// 如果要设置点击 popupwindow 以外的地方回收 popupwindow,就不要设置界面为灰色
// WindowManager.LayoutParams lp = PersonalDetailsActivity.this.getWindow().getAttributes();
// lp.alpha = 0.7f;
// PersonalDetailsActivity.this.getWindow().setAttributes(lp);

// 设置 popupwindow 的弹出和回收动画
// popupWindow.setAnimationStyle(R.style.PopAnimation);
// popupwindow 获取焦点 锁定后面的界面。如果 popupwindow 只占屏幕的一部分,点击 popupwindow 以外的控件(按钮)不想让它响应就设置这个属性
// popupWindow.setFocusable(true);
// 设置阴影(相当于背景颜色)
popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.c_60000000)));
// popupWindow.setBackgroundDrawable(new PaintDrawable());
// 点击 popupwindow 以外的地方和返回键 可以消失
popupWindow.setOutsideTouchable(true);  
// popupwindow 放在界面的下面
popupWindow.showAtLocation(ll, Gravity.BOTTOM, 0, 0);
// popupwindow 显示在屏幕的中央
// popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        
head_portrait_popupwindow_item.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        popupWindow.dismiss();
    }
});

head_portrait_popupwindow_item.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        popupWindow.dismiss();

        // popupWindow 回收后,界面的灰色消失
        // WindowManager.LayoutParams lp = PersonalDetailsActivity.this.getWindow().getAttributes();
        // lp.alpha = 1f;
        // PersonalDetailsActivity.this.getWindow().setAttributes(lp);
    }
});

相关文章

网友评论

      本文标题:PopupWindow的简单使用(从底部弹出和回收)

      本文链接:https://www.haomeiwen.com/subject/oopmvqtx.html