其他分享
首页 > 其他分享> > Android 自定义透明的Activity做为提示框

Android 自定义透明的Activity做为提示框

作者:互联网

一.在values目录中创建color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent">#0000</color> <!-- Color value must be #rgb, #argb, #rrggbb, or #aarrggbb -->
</resources> 
二.在values目录中创建style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="transparent">
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
</style>
</resources> 
三.java代码

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsoluteLayout;
import android.widget.TextView;
@SuppressWarnings("deprecation")
public class notify extends Activity
{
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // No Title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        //设置背景
        getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.msgbox));
        Bitmap bitmapDial = BitmapFactory.decodeResource(getResources(),R.drawable.msgbox);
        LayoutParams param = getWindow().getAttributes();//获取对话框当前的参数值
        param.width = bitmapDial.getWidth(); 
        param.height = bitmapDial.getHeight();
        getWindow().setAttributes((android.view.WindowManager.LayoutParams)param);//设置生效
        //确定界面的布局
        AbsoluteLayout abslayout=new AbsoluteLayout (this);
        setContentView(abslayout);
        // 提示信息
        TextView tMsg = new TextView(this);
        tMsg.setText(R.string.success);
        tMsg.setTypeface(Typeface.create("宋体", Typeface.BOLD_ITALIC));
        tMsg.setTextSize(18*getWindow().getWindowManager().getDefaultDisplay().getWidth()/main.nDefaultWidth);
        tMsg.setGravity(Gravity.CENTER);
        //确定这个控件的大小和位置
        AbsoluteLayout.LayoutParams lpParam = new AbsoluteLayout.LayoutParams(bitmapDial.getWidth(),bitmapDial.getHeight(),0,0);
        abslayout.addView(tMsg, lpParam);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
     finish();
     return super.onTouchEvent(event);
    }
} 
四.在AndroidMainfest.xml中声明
<activity android:name="notify" android:theme="@style/transparent"></activity>

 

标签:tMsg,自定义,LayoutParams,getWindow,import,Android,提示框,android,view
来源: https://blog.51cto.com/u_15298588/3034421