Android 自定義加載中對話框(簡易版)


話不多說,直接上圖

效果圖? ?

先編輯style文件:

<style name="dialog1" parent="@android:style/Theme.Dialog">
????<item name="android:windowFrame">@null</item>
? ? <item name="android:windowIsFloating">true</item>
????<item name="android:windowIsTranslucent">false</item>
????<item name="android:windowNoTitle">true</item>
????<item name="android:windowBackground">@android:color/transparent</item>? ?
????<item name="android:windowContentOverlay">@null</item>
</style>

然后,自定義一個LoadDialog.class類:

public class LoadDialog extends Dialog {
????private TextView tvContent;
? ? public LoadDialog(@NonNull Context context) {
????????super(context, R.style.dialog_loading);
????????setContentView(R.layout.dialog_load);
? ? ? ? setCanceledOnTouchOutside(false);
? ? ? ? tvContent = findViewById(R.id.tvDialogContent);
????}
? ? public void showloading(String content) {
????????tvContent.setText(content);
? ? ? ? super.show();
????}
? }

新建布局文件:dialog_load.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
????android:layout_width="match_parent"
? ? android:layout_gravity="center"
? ? android:layout_height="match_parent">
? ? <TextView
? ? ????android:id="@+id/tvDialogContent"
? ? ????android:layout_width="wrap_content"
????????android:layout_height="wrap_content"
? ? ? ? android:layout_centerInParent="true"
? ? ? ? android:background="#50000000"
? ? ????android:padding="@dimen/dp_10"
? ? ? ? android:text="加載中"
? ? ? ? android:textColor="#FFFFFF" />
</RelativeLayout>

使用:MainActivity.class中:

new?LoadDialog(this). showloading("加載中...");

但這種樣式貌似不太好看,我們給他變換一下textView 的樣式,新建load_style.xml,放在 drawable文件夾中

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
? ? <!--色值-->
? ? <solid android:color="#50000000" />
? ? <!--圓角-->
? ? <corners android:radius="5dp" />
</shape>

將TextView的android:background="#50000000" ,替換成:android:background="@drawable/load_style"

稍作修改后的效果圖
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容