本文实例讲述了Android编程实现对话框Dialog背景透明功能。分享给大家供大家参考,具体如下:
先看效果:
这是我做的一个拨号器强的面板,拨号的时候会查询手机中的联系人,显示在拨号面板上方,点击弹出透明对话框供选择。
这次重点是透明对话框。
先看对话框的theme,style文件:
代码语言:javascript复制<?xml version="1.0" encoding="utf-8"?
<resources
<style
name="selectorDialog"
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" @drawable/selector_dialog_bg</item <!--背景透明--
<item name="android:backgroundDimEnabled" false</item <!--模糊--
<item name="android:backgroundDimAmount" 0.6</item
</style
</resources
对话框背景@drawable/selector_dialog_bg:
代码语言:javascript复制<?xml version="1.0" encoding="utf-8"?
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
<solid android:color="#333333"/
<stroke
android:width="2dp"
android:color="#99CC33" /
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" /
<corners android:radius="8dp" /
</shape
然后是对话框的布局:
代码语言:javascript复制<?xml version="1.0" encoding="utf-8"?
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
<ListView
android:id="@ id/selector_dialog_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:scrollbars="none"
android:dividerHeight="1.0dip"
android:divider="#C4C4C4" /
</LinearLayout
程序中:
代码语言:javascript复制final View view = LayoutInflater.from(this).inflate(R.layout.selector_dialog, null);
selectorDialog = new Dialog(DialerActivity.this, R.style.selectorDialog);
selectorDialog.setContentView(view);
final BaseAdapter adapter = new SelectorAdapter(DialerActivity.this, selectorList);
ListView listView = (ListView) view.findViewById(R.id.selector_dialog_listview);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<? parent, View view, int position, long id) {
//对话框中事件处理
}
});
listView.setAdapter(adapter);
selectorDialog.show();
selectorDialog.setCanceledOnTouchOutside(true);
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = selectorDialog.getWindow().getAttributes();
lp.width = (int)(display.getWidth() * 0.9);
if(selectorList.size() 7) {
lp.height = (int)(display.getHeight() * 0.9);
}
lp.alpha = 0.8f;
selectorDialog.getWindow().setAttributes(lp);
其实主要是通过WindowManager.LayoutParams
给对话框设置属性。
希望本文所述对大家Android程序设计有所帮助。