Android實現可使用自定義透明Dialog樣式的Activity完整實例
本文實例講述了Android實現可使用自定義透明Dialog樣式的Activity。分享給大家供大家參考,具體如下:
有時你需要一個對話框,但同時對話框中的內容有更多控制和能控制其生命周期,這時你可以使用帶有Dialog樣式的Activity來應用你的項目中,想使Activity有對話框那樣效果可以在Androidmanifest中添加 Android:style/Theme.Dialog 的主題特性
例如這樣:
<activity android:name="MyDialogActivity" android:theme="@android:style/Theme.Dialog"> </activity>
如果你覺得上面不足以滿足你的需求想實現 半透明的, 圓角的,不妨可以試試自定義對話框的style
首先
第一步: 準備工作要做好 一張.9圖片
loading_bg.9.png
第二步 :自定義style 在你項目 values 文件夾中創(chuàng)建一個styles.xml 文件
接著在styles文件中添加 名為 MyDialogStyle的style
<style name="MyDialogStyle"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:backgroundDimEnabled">true</item> </style>
第三步:Dialog的布局 在layout文件夾中添加布局文件 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="180dp" android:layout_height="180dp" android:layout_centerInParent="true" android:background="@drawable/loading_bg" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:gravity="center" android:orientation="vertical" > <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="正在登錄" android:textColor="#fff" android:textSize="20sp" /> </LinearLayout> </RelativeLayout> </RelativeLayout>
創(chuàng)建 DialogActivity類
public class DialogActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 這里你可以進行一些等待時的操作,我這里用8秒后顯示Toast代理等待操作 new Handler().postDelayed(new Runnable(){ @Override public void run(){ DialogActivity.this.finish(); Toast.makeText(getApplicationContext(), "登錄成功", Toast.LENGTH_SHORT).show(); } }, 8000); } }
第四步 : 在Manifest文件中對DialogActivity進行注冊,并使用上面自定義MyDialogStyle樣式
<activity android:name="LoadingActivity" android:theme="@style/MyDialogStyle" />
第五步:運行工程并查看效果圖
打完收工
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android編程之activity操作技巧總結》、《Android控件用法總結》、《Android視圖View技巧總結》、《Android文件操作技巧匯總》、《Android操作SQLite數據庫技巧總結》、《Android操作json格式數據技巧總結》、《Android數據庫操作技巧總結》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進階教程》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設計有所幫助。
- Android編程自定義對話框(Dialog)位置及大小的方法
- Android中自定義對話框(Dialog)的實例代碼
- android dialog自定義實例詳解
- 實例詳解Android自定義ProgressDialog進度條對話框的實現
- Android 去掉自定義dialog的白色邊框的簡單方法
- Android自定義ProgressDialog進度等待框
- Android使用自定義alertdialog實現確認退出按鈕
- Android 自定義ProgressDialog進度條對話框用法詳解
- Android編程之自定義AlertDialog(退出提示框)用法實例
- Android實現自定義圓角對話框Dialog的示例代碼
- Android編程實現自定義Dialog的大小自動控制方法示例
相關文章
Android Studio 合并module到統一文件夾的方法
這篇文章主要介紹了Android Studio 合并module到統一文件夾的方法,補充介紹了android studio關于同名資源文件的合并技巧,需要的朋友可以參考下2018-04-04Kotlin + Flow 實現Android 應用初始化任務啟動庫
這篇文章主要介紹了Kotlin + Flow 實現Android 應用初始化任務啟動庫的方法,幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下2021-03-03Android 中FloatingActionButton(懸浮按鈕)實例詳解
這篇文章主要介紹了Android 中FloatingActionButton(懸浮按鈕)實例詳解的相關資料,需要的朋友可以參考下2017-05-05Android 8.0不能自動安裝APK問題的解決方法(完美適配)
這篇文章主要給大家介紹了關于Android 8.0不能自動安裝APK問題的解決方法(完美適配),這里的自動安裝是指下載完成后,自動彈出安裝界面,而不是靜默安裝APK,文中介紹的非常詳細,需要的朋友可以參考下2018-07-07