效果图:
核心代码:
MainActivity中启动DialogActivity
<span style="font-size:18px;">public class MainActivity extends Activity {
private Button openBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openBtn = (Button) findViewById(R.id.open_btn);
openBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), DialogActivity.class);
startActivity(intent);
}
});
}
}</span>
在DialogActivity中
<span style="font-size:18px;">public class DialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
}</span>
DialogActivity布局文件activity_dialog.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_dialog"
android:layout_width="280dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/dialog_theme"
android:gravity="center"
android:minWidth="200dip"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="我是activity"
android:gravity="center"
android:textSize="30dp"
/>
</LinearLayout></span>
res----drawable----dialog_theme.xml设置对话框的圆角等
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 填充颜色 -->
<solid android:color="#ffffffff" />
<!-- 边框宽度 -->
<!-- <stroke android:width="3dp" /> -->
<!-- 圆角半径 -->
<corners android:radius="10dp" />
<!-- 内边距 -->
<padding
android:bottom="13dp"
android:left="13dp"
android:right="13dp"
android:top="13dp" />
</shape></span>
res----values----styles.xml中设置样式
<span style="font-size:18px;"> <style name="dialogStyle" parent="@android:style/Theme.Dialog">
<!-- 边框 -->
<item name="android:windowFrame">@null</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 半透明 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 无标题 -->
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<!-- 背景透明 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 模糊 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 遮罩层 -->
<item name="android:backgroundDimAmount">0.3</item>
</style></span>
主清单配置文件中AndroidManifest.xml
只要引用样式android:theme="@style/dialogStyle"就可以吧activity设置成对话框了
<span style="font-size:18px;"> <!-- 只要引用样式android:theme="@style/dialogStyle"就可以吧activity设置成对话框了 -->
<activity
android:name="com.example.diaglogactivitydemo.DialogActivity"
android:screenOrientation="portrait"
android:theme="@style/dialogStyle" >
</activity></span>
源码下载:
http://download.csdn.net/detail/zhaihaohao1/9551680