Android編程實現(xiàn)對話框Dialog背景透明功能示例
本文實例講述了Android編程實現(xiàn)對話框Dialog背景透明功能。分享給大家供大家參考,具體如下:
先看效果:
這是我做的一個撥號器強的面板,撥號的時候會查詢手機中的聯(lián)系人,顯示在撥號面板上方,點擊彈出透明對話框供選擇。
這次重點是透明對話框。
先看對話框的theme,style文件:
<?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><!--是否浮現(xiàn)在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:
<?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>
然后是對話框的布局:
<?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>
程序中:
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
給對話框設(shè)置屬性。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android編程實現(xiàn)圖片背景漸變切換與圖層疊加效果
- Android編程實現(xiàn)左右滑動切換背景的方法
- Android實現(xiàn)動態(tài)切換組件背景的方法
- 修改Android FloatingActionButton的title的文字顏色及背景顏色實例詳解
- Android編程實現(xiàn)控件不同狀態(tài)文字顯示不同顏色的方法
- Android中EditText和AutoCompleteTextView設(shè)置文字選中顏色方法
- Android開發(fā)中ImageLoder加載網(wǎng)絡(luò)圖片時將圖片設(shè)置為ImageView背景的方法
- android開發(fā)修改狀態(tài)欄背景色和圖標顏色的示例
- Android中創(chuàng)建類似Instagram的漸變背景效果
- Android開發(fā)之背景動畫簡單實現(xiàn)方法
- Android開發(fā)實現(xiàn)按鈕點擊切換背景并修改文字顏色的方法
相關(guān)文章
Android的Activity跳轉(zhuǎn)動畫各種效果整理
Android的Activity跳轉(zhuǎn)就是很生硬的切換界面。其實Android的Activity跳轉(zhuǎn)可以設(shè)置各種動畫,本文整理了一些,還有很多動畫效果,就要靠我們發(fā)揮自己的想象力2013-06-06Android自定義ViewGroup嵌套與交互實現(xiàn)幕布全屏滾動
這篇文章主要為大家介紹了Android自定義ViewGroup嵌套與交互實現(xiàn)幕布全屏滾動效果示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01ProgressBar、ProgessDialog-用法(詳解)
下面小編就為大家?guī)硪黄狿rogressBar、ProgessDialog-用法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06Android開發(fā)實現(xiàn)的ViewPager引導頁功能(動態(tài)加載指示器)詳解
這篇文章主要介紹了Android開發(fā)實現(xiàn)的ViewPager引導頁功能(動態(tài)加載指示器),結(jié)合實例形式詳細分析了Android使用ViewPager引導頁的具體步驟,相關(guān)布局、功能使用技巧,需要的朋友可以參考下2017-11-11