您的位置:首页 > 移动开发 > Android开发

Android开发:自定义对话框(自定义布局及背景透明等)

2013-08-21 14:25 489 查看
直接从android.app.Dialog.Dialog类自定义,代码如下:

final Dialog dialog = new Dialog(MainActivity.this, R.style.UpdateDialog);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.update_dialog, null);
dialog.setContentView(view);

Button btn_yes = (Button)view.findViewById(R.id.update_yes);
Button btn_no = (Button)view.findViewById(R.id.update_no);

btn_yes.setOnClickListener(new OnClickListener(){

public void onClick(View v) {
dialog.dismiss();
}

});
btn_no.setOnClickListener(new OnClickListener(){

public void onClick(View v) {

dialog.dismiss();
}

});

dialog.show();


R.style.UpdateDialog在styles.xml中进行定义,如下:

<resources>

<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

<style
name="UpdateDialog"
parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item><!--边框-->
<item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">false</item><!--半透明-->
<item name="android:windowNoTitle">true</item><!--无标题-->
<item name="android:windowBackground">@color/trans</item><!--背景透明-->
<item name="android:backgroundDimEnabled">true</item><!--模糊-->
<item name="android:backgroundDimAmount">0.0</item>
</style>

</resources>


在配置文件中可设置对话框的多个属性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: