实现方法是利用一个全屏的dialog加载一个带有半透明背景layout
具体实现方法如下:
首先,设置要覆盖在最上方的dialog的view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_transparent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_tips"
android:layout_centerHorizontal="true"
android:text="长按某一项弹出\n编辑、删除选项按钮\n\n\n\n知道啦~\n点我退出提示 ^_^"
android:textColor="@color/white"
android:gravity="center_horizontal"
android:textSize="27sp" />
<ImageView
android:id="@+id/iv_tips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_marginLeft="78dp"
android:layout_marginTop="154dp"
android:src="@drawable/longclick" />
</RelativeLayout>
然后,在values/styles.xml添加style for 半透明
<style name="Dialog_Fullscreen">
<item name="android:windowBackground">@android:color/transparent
</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item
name="android:windowAnimationStyle">@style/Dialog_Anim
</item>
<item name="android:colorBackgroundCacheHint">@null</item>
</style>
继续添加style for 淡入淡出效果:
<style name="Dialog_Anim">
<item name="android:windowEnterAnimation">@anim/fade_in</item>
<item name="android:windowExitAnimation">@anim/fade_out</item>
</style>
在anim文件夹下,添加文件fade_in.xml for 淡入
<?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="0.001"
android:toXScale="1.0"
android:fromYScale="0.001"
android:toYScale="1.0"
android:pivotX="0%"
android:pivotY="100%"
android:duration="300" />
添加文件fade_out.xml for 淡出
<?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="0.001"
android:fromYScale="1.0"
android:toYScale="0.001"
android:pivotX="100%"
android:pivotY="100%"
android:duration="300" />
最后,在要弹出对话框的地方添加如下代码
//显示提示信息
private void showTips(){
final Dialog dialog = new Dialog(this, R.style.Dialog_Fullscreen);
dialog.setContentView(R.layout.tips_for_long_click_layout);
dialog.show();
TextView tv = (TextView)dialog.findViewById(R.id.textView1);
String fontPath = "fonts/hkwwt.TTF";
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
tv.setTypeface(tf);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
其中字体为华康娃娃体,下面是一些常用的字体:
http://pan.baidu.com/s/1pLGP8ZT
实现效果如下: