activity 当做弹框也是可以使用的。
首先创建一个activity。但是主要的是继承AppCompatActivity是不行的,汇报下面的错误。需要修改成Activity。原因下面说。
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:322)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:283)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.wangjisen.activitypop.PopActivity.onCreate(PopActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
为什么会报错呢?因为我们创建好activity后需要修改activity的theme属性,目前我们采用自定义的形式。
继续查看DialogActivityStyle
activity的theme属性就设置好了。
完整activity:
package com.example.wangjisen.activitypop;
import android.app.Activity;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Toast;
public class PopActivity extends Activity {
private ConstraintLayout cl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop);
cl = findViewById(R.id.cl);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.cl:
Toast.makeText(this, "弹框", Toast.LENGTH_SHORT).show();
break;
case R.id.button_cancel:
Toast.makeText(this, "取消", Toast.LENGTH_SHORT).show();
finish();
break;
case R.id.button_confirm:
finish();
Toast.makeText(this, "确定", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
//点击退出
@Override
public boolean onTouchEvent(MotionEvent event) {
finish();
return true;
}
}
对应的activity_pop.xml
当做一个菜单使用:
style:MyMenuTopRight
动画:
scale_in
scale_out
完整的PopActivity
package com.example.wangjisen.activitypop;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MenuActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
LinearLayout ll = findViewById(R.id.ll);
WindowManager.LayoutParams params=getWindow().getAttributes();
params.x=600;
params.y=-570;
getWindow().setAttributes(params);
}
public void myClick(View view) {
switch (view.getId()) {
case R.id.tv_1:
Toast.makeText(this, "粘贴", Toast.LENGTH_SHORT).show();
finish();
break;
case R.id.tv_2:
Toast.makeText(this, "复制", Toast.LENGTH_SHORT).show();
finish();
break;
case R.id.tv_3:
Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show();
finish();
break;
case R.id.ll:
Toast.makeText(this, "菜单", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
finish();
return true;
}
}
布局文件:activity_menu
完整的MainActivity
package com.example.wangjisen.activitypop;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
intent = new Intent(this, PopActivity.class);
startActivity(intent);
break;
case R.id.button_menu:
intent = new Intent(this, MenuActivity.class);
startActivity(intent);
break;
}
}
}
布局activity_main
鸣谢:https://blog.csdn.net/lmj623565791/article/details/23116115