Android Activity 伪弹框的基本使用
Posted 大不懂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Activity 伪弹框的基本使用相关的知识,希望对你有一定的参考价值。
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属性,目前我们采用自定义的形式。
<activity
android:name=".PopActivity"
android:theme="@style/DialogActivityStyle">
继续查看DialogActivityStyle
<style name="DialogActivityStyle">
<!--设置dialog的背景-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--设置Dialog的windowFrame框为无-->
<item name="android:windowFrame">@null</item>
<!--设置无标题-->
<item name="android:windowNoTitle">true</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否半透明-->
<item name="android:windowIsTranslucent">true</item>
<!--设置窗口内容不覆盖-->
<item name="android:windowContentOverlay">@null</item>
<!--设置动画,在这里使用让它继承系统的Animation.Dialog-->
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<!--背景是否模糊显示-->
<item name="android:backgroundDimEnabled">true</item>
</style>
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
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="300dp"
android:layout_height="180dp"
android:background="@drawable/bg_popup"
android:id="@+id/cl"
android:onClick="onClick"
tools:context="com.example.wangjisen.activitypop.PopActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="我是pop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="30dp"
android:onClick="onClick"
android:text="取消"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv" />
<Button
android:id="@+id/button_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginRight="30dp"
android:onClick="onClick"
android:text="确定"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv" />
</android.support.constraint.ConstraintLayout>
当做一个菜单使用:
<activity
android:name=".MenuActivity"
android:theme="@style/MyMenuTopRight"></activity>
style:MyMenuTopRight
<style name="MyMenuTopRight">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<!--设置Dialog的windowFrame框为无-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--设置窗口内容不覆盖-->
<item name="android:windowContentOverlay">@null</item>
<!--背景是否模糊显示-->
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowAnimationStyle">@style/Anim_scale</item>
</style>
<style name="Anim_scale" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">@anim/scale_in</item>
<item name="android:activityOpenExitAnimation">@anim/scale_out</item>
<item name="android:activityCloseEnterAnimation">@anim/scale_in</item>
<item name="android:activityCloseExitAnimation">@anim/scale_out</item>
</style>
动画:
scale_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:fromXScale="1.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toXScale="1.0"
android:toYScale="1.0"
android:duration="200"
android:pivotX="0"
android:pivotY="300"
/>
</set>
scale_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toXScale="1.0"
android:toYScale="0.0"
android:duration="200"
android:pivotX="0"
android:pivotY="900"
/>
</set>
完整的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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/menu_bg"
android:onClick="myClick"
android:orientation="vertical">
<TextView
android:onClick="myClick"
android:id="@+id/tv_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:drawableLeft="@drawable/abc_ic_menu_paste_mtrl_am_alpha"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="粘贴"
android:textColor="#fff"
android:textSize="20sp" />
<TextView
android:onClick="myClick"
android:id="@+id/tv_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:drawableLeft="@drawable/abc_ic_menu_copy_mtrl_am_alpha"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="复制"
android:textColor="#fff"
android:textSize="20sp" />
<TextView
android:onClick="myClick"
android:id="@+id/tv_3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:drawableLeft="@drawable/abc_ic_menu_share_mtrl_alpha"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="分享"
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>
完整的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
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.wangjisen.activitypop.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="弹出"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_menu"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:onClick="onClick"
android:text="菜单"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
鸣谢:https://blog.csdn.net/lmj623565791/article/details/23116115
以上是关于Android Activity 伪弹框的基本使用的主要内容,如果未能解决你的问题,请参考以下文章
android将activity设置成自定义的Dialog怎么调整大小