1. styles.xml 中加入
<style name="loadingDialogStyle" parent="android:Theme.Dialog"> <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>
2. dialog_loading.xml
<?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="match_parent" android:background="@android:color/transparent" android:gravity="center" android:orientation="vertical" > <LinearLayout android:id="@+id/LinearLayout" android:layout_width="160dp" android:layout_height="160dp" android:background="@drawable/dialog_bg" android:gravity="center" android:orientation="vertical" > <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@android:color/transparent" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingTop="10dp" android:textColor="@android:color/white" /> </LinearLayout> </LinearLayout>
3. LoadingDialog.java
public class LoadingDialog extends Dialog { private TextView tv; public LoadingDialog(Context context) { super(context, R.style.loadingDialogStyle); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_loading); tv = (TextView) this.findViewById(R.id.tv); tv.setText("正在登录..."); LinearLayout linearLayout = (LinearLayout) this .findViewById(R.id.LinearLayout); linearLayout.getBackground().setAlpha(210); } }
4. 加入你的主代码中
public class MainActivity extends Activity { LoadingDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dialog = new LoadingDialog(this); dialog.setCanceledOnTouchOutside(false); dialog.show(); } }
5. 总结
相当于自定义一个界面,中间一个dialog,背景setAlpha(210),覆盖你的界面。登录成功后,消失dialog.dismiss().
时间: 2024-10-12 08:32:01