Android開發(fā)之利用Activity實(shí)現(xiàn)Dialog對(duì)話框
前言
在Android中經(jīng)常要使用Dialog來實(shí)現(xiàn)一些提示以及一些特殊的效果,而且樣式也不一樣,每次都得查一大堆資料,還不一定能解決。對(duì)話框是個(gè)好東西,創(chuàng)建簡(jiǎn)單有實(shí)用。當(dāng)下的開發(fā)中,很多的開發(fā)者反而更喜歡使用activity來代替對(duì)話框,至少筆者的團(tuán)隊(duì)中,類似于升級(jí)提示或者指示頁及其他一些交互的地方,大量的把Dialog替換成activity,好處是顯而易見的,activity具有更靈活的操作和布局,另外很重要一點(diǎn)是,一些容易涉及內(nèi)存泄漏的代碼放在activity中執(zhí)行比放在Dialog中執(zhí)行要好的多,當(dāng)然這是筆者自己的觀點(diǎn),文中有不對(duì)的地方,歡迎大家提出指正,好讓筆者及時(shí)改正,共同學(xué)習(xí)。
先上效果圖:
實(shí)現(xiàn)方法
這個(gè)對(duì)話框常常能在一些APP中遇到,首先是布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="300dp" android:layout_height="400dp" android:layout_gravity="center" android:background="@drawable/popup_bg" android:orientation="vertical" tools:context="com.yankee.september_2.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/btn_update" android:layout_marginBottom="24dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Version" android:textColor="#53BAF8" android:textSize="14sp" /> <ScrollView android:layout_width="match_parent" android:layout_height="100dp" android:layout_gravity="center_horizontal" android:layout_marginTop="12dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Nowadays, when it comes to the issues of robots, individuals' opinions vary from person to person。 Some people believe that robots will enlighten our life, while other are worried about that they will ruin the whole world。 As I see, the increasing number of robots has the power to alter everything in the coming future entirely。" android:textColor="#404040" android:textSize="12sp" /> </ScrollView> </LinearLayout> <Button android:id="@+id/btn_update" android:layout_width="130dp" android:layout_height="32dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="16dp" android:background="@color/colorAccent" android:gravity="center" android:text="Update now" android:textColor="#FFFFFF" android:textSize="14sp" /> </RelativeLayout>
布局代碼中需要注意的是,這個(gè)布局的大小是寫死的,因?yàn)閺拇a中可以看出,對(duì)話框的鏤空效果其實(shí)是一張背景圖產(chǎn)生的,辛苦美術(shù)但是爽了程序員。
第二步:接著就是在manifest文件的操作了,把對(duì)應(yīng)的activity的theme設(shè)置為自定義的主題即可,背景設(shè)為透明,去掉標(biāo)題,注意,這個(gè)主題繼承自對(duì)話框的主題。
manifest:
<activity android:name=".MainActivity" android:screenOrientation="portrait" android:theme="@style/TipDialog"> </activity>
styles.xml:
<style name="TipDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> </style>
最后就沒有最后了,還有一點(diǎn),就是讓當(dāng)前這個(gè)activity繼承自Activity,而不是AppCompatActivity,否則會(huì)報(bào)主題的錯(cuò)誤。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
Android提高之多級(jí)樹形菜單的實(shí)現(xiàn)方法
這篇文章主要介紹了Android多級(jí)樹形菜單的實(shí)現(xiàn)方法,很實(shí)用的功能,需要的朋友可以參考下2014-08-08Android通過SeekBar調(diào)節(jié)布局背景顏色
這篇文章主要為大家詳細(xì)介紹了Android通過SeekBar調(diào)節(jié)布局背景顏色,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04完美解決客戶端webview持有的頁面緩存,不會(huì)立即釋放的問題
下面小編就為大家?guī)硪黄昝澜鉀Q客戶端webview持有的頁面緩存,不會(huì)立即釋放的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12Android開發(fā)必知 九種對(duì)話框的實(shí)現(xiàn)方法
App中少不了與用戶交互的各種dialog,以此達(dá)到很好的用戶體驗(yàn),下面給大家介紹Android開發(fā)必知 九種對(duì)話框的實(shí)現(xiàn)方法,有需要的朋友可以參考下2015-08-08Android三種方式實(shí)現(xiàn)ProgressBar自定義圓形進(jìn)度條
這篇文章主要介紹了Android三種方式實(shí)現(xiàn)ProgressBar自定義圓形進(jìn)度條的相關(guān)資料,需要的朋友可以參考下2016-03-03autojs模仿QQ長按彈窗菜單實(shí)現(xiàn)示例
這篇文章主要為大家介紹了autojs模仿QQ長按彈窗菜單實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android報(bào)錯(cuò)Didn‘t?find?class?“android.view.x“問題解決原理剖析
這篇文章主要為大家介紹了Android報(bào)錯(cuò)Didn‘t?find?class?“android.view.x“問題解決及原理剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03