package com.loaderman.activitydialogdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //设置标题栏右侧按钮的作用 public void btnmainright(View v) { System.out.println(1); Intent intent = new Intent (this,MainTopRightDialog.class); startActivity(intent); Toast.makeText(getApplicationContext(), "点击了功能按钮", Toast.LENGTH_LONG).show(); } }
package com.loaderman.activitydialogdemo; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.LinearLayout; import android.widget.Toast; public class MainTopRightDialog extends Activity { private LinearLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_top_right_dialog); layout=(LinearLayout)findViewById(R.id.main_dialog_layout); layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "提示:点击窗口外部关闭窗口!", Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onTouchEvent(MotionEvent event){ finish(); return true; } }
在res/anim下创建弹窗的进出动画:
push_top_in2.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 上下滑入式 --> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="1.0" android:toXScale="1.0" android:fromYScale="0" android:toYScale="1.0" android:pivotX="0" android:pivotY="10%" android:duration="200" />
push_top_out2.xml
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="1.0" android:toXScale="1.0" android:fromYScale="1.0" android:toYScale="0" android:pivotX="0" android:pivotY="10%" android:duration="200" />
acrivity_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" tools:context="com.loaderman.activitydialogdemo.MainActivity"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="45dp" android:id="@+id/title" android:background="@drawable/title_bar" android:gravity="center_vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="微信" android:layout_centerInParent="true" android:textSize="20sp" android:textColor="#ffffff" /> <ImageButton android:id="@+id/right_btn" android:layout_width="67dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:src="@drawable/title_btn_function" android:background="@drawable/title_btn_right" android:onClick="btnmainright" /> </RelativeLayout> </RelativeLayout>
activity_main_top_right_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main_top_right_dialog" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="46dp" > <LinearLayout android:id="@+id/main_dialog_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:background="@drawable/title_function_bg" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="8dp" android:src="@drawable/mm_title_btn_compose_normal"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="发起聊天" android:textColor="#fff" android:textSize="18sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="8dp" android:src="@drawable/mm_title_btn_receiver_normal"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="听筒模式" android:textColor="#fff" android:textSize="18sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="8dp" android:src="@drawable/mm_title_btn_keyboard_normal"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="登录网页版" android:textColor="#fff" android:textSize="18sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/imageView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="8dp" android:src="@drawable/mm_title_btn_qrcode_normal"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="扫一扫" android:textColor="#fff" android:textSize="18sp"/> </LinearLayout> </LinearLayout> </RelativeLayout> </RelativeLayout>
需要给MainTopRightDialog设置style
<activity android:name=".MainTopRightDialog" android:theme="@style/MyDialogStyleTop"/>
<style name="MyDialogStyleTop" parent="android:Theme.Dialog" > <item name="android:windowAnimationStyle">@style/AnimTop2</item> <item name="android:windowFrame">@null</item><!--边框--> <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上--> <item name="android:windowIsTranslucent">true</item><!--半透明--> <item name="android:windowNoTitle">true</item><!--无标题--> <item name="android:windowBackground">@android:color/transparent</item><!--背景透明--> <item name="android:backgroundDimEnabled">false</item><!--模糊--> </style> <style name="AnimTop2" parent="@android:style/Animation"> <item name="android:windowEnterAnimation">@anim/push_top_in2</item> <item name="android:windowExitAnimation">@anim/push_top_out2</item> </style>
效果图: