如何使用Activity创建对话框

本示例使用Android系统对话框主题样式来创建一个对话框。

1.定义清单文件(AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="my.android.test"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<!-- Activity应用系统的对话框样式Theme.Dialog -->

<activity android:name=".DialogActivity"

android:label="@string/app_name"

android:theme="@android:style/Theme.Dialog">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

<uses-sdk android:minSdkVersion="9" />

</manifest>

2.定义字符串资源(strings.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="hello">Hello World, DialogActivity!</string>

<string name="app_name">DialogActivity</string>

<string name="dialog_activity_text">Example of how you can use the

Theme.Dialog theme to make an activity that looks like a

dialog.</string>

</resources>

3.定义布局文件(dialog_activity.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- 用户显示对话框文本 -->

<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"

android:layout_width="match_parent" android:layout_height="match_parent"

android:gravity="center_vertical|center_horizontal"

android:text="@string/dialog_activity_text"/>

4.创建Activity类(DialogActivity.java

package my.android.test;

import android.app.Activity;

import android.os.Bundle;

import android.view.Window;

publicclass DialogActivity extends Activity {

/** Activity被首次创建时,调用该方法 */

@Override

publicvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//调用requestWindowFeature()方法启用扩展的窗口特征。

//Window.FEATURE_LEFT_ICON:设置标题栏的左下方的一个图标资源可用。

if(true == requestWindowFeature(Window.FEATURE_LEFT_ICON)){

//填充布局

setContentView(R.layout.dialog_activity);

//给窗口设定描画资源。

getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,

android.R.drawable.ic_dialog_alert);

}else{

//

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值