Android全屏对话框

1、Android全屏对话框(附带延时关闭效果)

http://www.linuxidc.com/Linux/2013-04/82444.htm

自定义style,设置全屏属性

<resources>

    <style name="AppTheme" parent="Android:Theme.Black"/>
    <style name="processDialog" >
        <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
        <item name="android:windowFullscreen">true</item> 
        <item name="android:windowIsTranslucent">false</item><!--半透明--> 
        <item name="android:windowNoTitle">true</item><!--无标题--> 
        <item name="android:windowBackground">@android:color/transparent</item><!--背景透明--> 
        <item name="android:backgroundDimEnabled">true</item><!--模糊--> 
        <item name="android:backgroundDimAmount">0.5</item>
        <item name="android:alpha">0.3</item>
    </style> 

</resources>

代码中加载这个view并把view set到dialog上,这样全屏的dialog就完成了

mView = LayoutInflater.from(this).inflate(R.layout.process_dialog, null); 
 processDialog = new Dialog(LogActivity.this, R.style.processDialog); 
 processDialog.setCancelable(false);
 processDialog.setContentView(mView); 
mAutoCloseDialog = new AutoCloseDialog(processDialog);
mAutoCloseDialog.show(Prefs.DIALOG_DISPLAY_TIME);

接下来用一个封装好的类,做一个延时关闭的效果

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


import android.app.Dialog;

public class AutoCloseDialog{ 
   
    private Dialog dialog; 
    private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); 
     
    public AutoCloseDialog(Dialog dialog){ 
        this.dialog = dialog; 
    } 
     
    public void show(long duration){ 
     
        Runnable runner = new Runnable() { 
            public void run() { 
                dialog.dismiss(); 
            } 
        }; 
        executor.schedule(runner, duration, TimeUnit.MILLISECONDS);
        dialog.show();
    } 
     
}


2、Android中自定义对话框(Dialog)的实例代码

http://www.jb51.net/article/40825.htm

1.修改系统默认的Dialog样式(风格、主题)

2.自定义Dialog布局文件

3.可以自己封装一个类,继承自Dialog或者直接使用Dialog类来实现,为了方便以后重复使用,建议自己封装一个Dialog类

 
第一步:

  我们知道Android定义个控件或View的样式都是通过定义其style来实现的,查看Android框架中的主题文件,在源码中的路径:/frameworks/base/core/res/res/values/themes.xml,我们可以看到,Android为Dialog定义了一个样式,

复制代码 代码如下:

<style name="Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item>
<item name="android:windowBackground">@android:drawable/panel_background</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>

我们可以看到,在Themes.xml中定义的Dialog的样式,其中,定义了window的标题样式,window的背景图,是否悬浮等等。

  那么,我们要创建具有自定义样式的Dialog就可以创建一个styles.xml,在其中定义我们自己的Dialog样式,让其继承自Theme.Dialog样式,并修改其中的某些属性即可。

  定义我们自己的Dialog样式:

  a.创建一个styles.xml文件,放在res/values 文件夹下(当然了,这就不用说了。。。啰嗦一下)

  b.在styles.xml中定义Dialog样式,代码如下:
复制代码 代码如下:

<style name="Theme_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>

  上面代码中,定义了一个样式Theme_dialog,继承自@android:style/Theme.Dialog,然后定义了Dialog所在Window的背景图,此处使用的是透明颜色#00000000,然后定义了Dialog所在的Window隐藏标题(系统定义Dialog样式是带有标题的,在此设置此属性为true可隐藏标题),自定义Dialog样式到这就完成了。

第二步:

  定义Dialog的布局:

  创建一个布局文件layout_dialog.xml,代码如下:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/pp_bg_dialog"
android:gravity="center">

<ProgressBar android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/progressbar_normal"/>

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
style="@style/text_white_small" android:layout_marginTop="5dp"
android:text="正在删除..." android:id="@+id/message"/>
</LinearLayout>

这里使用了一个垂直方向的线性布局,并且设置所有子元素居中,子元素为已个进度条ProgressBar和一个TextView。

  此处,ProgressBar采用自定义样式,使用系统默认的ProgressBar可达到同样的效果(大同小异)。LinearLayout的背景

android:background="@drawable/pp_bg_dialog"(即上面效果图中居中显示的黑色透明背景框)是一个自定义的图片资源Shape:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#55000000"/>

</shape>

代码中定义了一个矩形,并设置了圆角和颜色。到此,Dialog的布局就完成了。
第三步:
  自定义CustomDialog类,继承自Dialog,代码如下:
复制代码 代码如下:

public class CustomDialog extends Dialog { 2
private static int default_width = 160; //默认宽度
private static int default_height = 120;//默认高度
public CustomDialog(Context context, int layout, int style) {
this(context, default_width, default_height, layout, style);
}

public CustomDialog(Context context, int width, int height, int layout, int style) {
super(context, style);12
//set content
setContentView(layout);
//set window params
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
//set width,height by density and gravity
float density = getDensity(context);
params.width = (int) (width*density);
params.height = (int) (height*density);
params.gravity = Gravity.CENTER;
window.setAttributes(params);
}
private float getDensity(Context context) {
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
return dm.density;
}
}

在构造方法中设置了Dialog的contentView,并且设置了Window的宽度、高度和居中显示。

CustomDialog使用方法如下:
复制代码 代码如下:

public class CustomDialogDemoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
CustomDialog dialog1 = new CustomDialog(this, R.layout.common_dialog, R.style.Theme_dialog);//Dialog使用默认大小(160)
CustomDialog dialog2 = new CustomDialog(this, 180, 180, R.layout.common_dialog, R.style.Theme_dialog);
dialog2.show();//显示Dialog
    //如果要修改Dialog中的某个View,比如把"正在删除..."改为"加载中..."
TextView mMessage = (TextView) dialog2.findViewById(R.id.message);
mMessage.setText("加载中...");
}
}

3、android设立窗口全屏

http://www.doc100.net/bugs/t/825893/index.html

android设置窗口全屏
设置全屏包括两个部分: 窗口全屏和Activity全屏。

窗口全屏 是指隐藏系统顶部用来显示时间、电量、信号等信息的标题栏 。

Activity全屏 是指隐藏程序的标题栏。我们可以通过修改AndroidManifest.xml文件来实现。

(1)窗口全屏fullscreen.java代码如下:

package wzhnsc.test.style;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class fullscreen extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //不显示程序的标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        //不显示系统的标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,
                             WindowManager.LayoutParams. FLAG_FULLSCREEN);
        
        setContentView(R.layout.main);
    }
}


(2) 修改 AndroidManifest.xml

我们可以修改activity或application的属性值来实现。具体可以根据自己的情况来设置。

两者区别如下:

1)activity: 只针对当前的Activity全屏。

2)application:所有的Activity都会全屏。

AndroidManifest.xml 内容如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="wzhnsc.test.style"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon"
                 android:label="@string/app_name"
                 android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <activity android:name=".fullscreen"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="1" />

</manifest>




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值