自定义一个PopupWindow实现弹窗效果
效果图
1、CustomerPopupWindow.calss
package com.example.customerpopup; import android.os.Looper; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button btn; private CustomerPopupWindow popupWindow; private String content="如果在看你一眼,是否还会有感觉"; private String rate="80%的人已经更新该版本"; private String versionName="V 2.1.1"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn= (Button) findViewById(R.id.btn_popup); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow=new CustomerPopupWindow(content, rate, versionName, MainActivity.this, new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.dismiss(); popupWindow.backgroundAlpha(MainActivity.this,1f); /* Looper.prepare();//因为在事件中无法Toast,如要toast必须在toast前加Looper.prepare(),必须在Toast后添加Looper.loop()*/ Toast.makeText(MainActivity.this,"点击了立即升级",Toast.LENGTH_LONG).show(); //Looper.loop(); } }); popupWindow.showAtLocation(v, Gravity.CENTER,0,0); } }); } }
2、popupwindow.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="380dp" android:layout_marginLeft="32dp" android:layout_marginRight="32dp" android:layout_marginTop="16dp" android:layout_centerHorizontal="true" android:background="@mipmap/writeblue"> <TextView android:id="@+id/tv_version" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="16dp" android:layout_marginTop="160dp" android:text="V2.0.1" /> <TextView android:id="@+id/tv_udpate_notice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ee0000" android:layout_below="@+id/tv_version" android:layout_alignParentRight="true" android:layout_marginTop="10dp" android:layout_marginRight="16dp" android:textSize="14sp" android:text="80%的用户已升级"/> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_udpate_notice" android:layout_above="@+id/tv_update_now" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:textSize="14sp" android:lineSpacingExtra="8dp" android:text="1.产品xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n2.xxxxxxxxxxx\n3:xxxxx"/> <TextView android:gravity="center" android:id="@+id/tv_update_now" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#ff0000" android:layout_alignParentBottom="true" android:layout_marginBottom="16dp" android:text="立即升级" /> </RelativeLayout> <ImageView android:id="@+id/iv_close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:src="@mipmap/close"/> </RelativeLayout>
3、MainActivity.class
package com.example.customerpopup; import android.os.Looper; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button btn; private CustomerPopupWindow popupWindow; private String content="如果在看你一眼,是否还会有感觉"; private String rate="80%的人已经更新该版本"; private String versionName="V 2.1.1"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn= (Button) findViewById(R.id.btn_popup); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow=new CustomerPopupWindow(content, rate, versionName, MainActivity.this, new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.dismiss(); popupWindow.backgroundAlpha(MainActivity.this,1f); /* Looper.prepare();//因为在事件中无法Toast,如要toast必须在toast前加Looper.prepare(),必须在Toast后添加Looper.loop()*/ Toast.makeText(MainActivity.this,"点击了立即升级",Toast.LENGTH_LONG).show(); //Looper.loop(); } }); popupWindow.showAtLocation(v, Gravity.CENTER,0,0); } }); } } 4、activity_main.xml<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.customerpopup.MainActivity"> <Button android:id="@+id/btn_popup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="弹出自定义PopupWindow!" /> </RelativeLayout>