簡體   English   中英

警告對話框未全屏顯示

[英]Alert dialog is not showing in full screen

我的“no inte.net”警報對話框沒有全屏顯示,而且我的按鈕在我點擊后不刷新就消失了。我應該怎么做才能將其更改為全屏打開,我也嘗試過使用 setContentView , 但它沒有用..

這是我的代碼..

Class 檔案:

public class NetworkChangeListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(!Common.isConnectedToInternet(context)){
            AlertDialog.Builder builder =  new AlertDialog.Builder(context);
            View view = LayoutInflater.from(context).inflate(R.layout.alert_dialog,null);
            builder.setView(view);

            AppCompatButton btnRetry = view.findViewById(R.id.btnRetry);

            AlertDialog dialog = builder.create();
            dialog.show();
            dialog.setCancelable(false);
            dialog.getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

            btnRetry.setOnClickListener(view1 -> {
                dialog.dismiss();
                onReceive(context,intent);
            });
        }
    }
}

XML 檔案:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f7f7f7"
    android:id="@+id/nointernet">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="212dp"
        android:layout_height="188dp"
        android:scaleType="centerCrop"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="250dp"
        android:src="@drawable/ic_baseline_signal_wifi_off_24"></ImageView>

    <TextView
        android:id="@+id/text"
        android:layout_width="253dp"
        android:layout_height="52dp"
        android:text="Error!"
        android:textAlignment="center"
        android:textSize="25dp"
        android:layout_below="@id/imageView"
        android:layout_centerInParent="true"></TextView>

    <TextView
        android:id="@+id/text2"
        android:layout_width="337dp"
        android:layout_height="63dp"
        android:text="No internet connection"
        android:textAlignment="center"
        android:textSize="21dp"
        android:layout_below="@id/text"
        android:layout_centerInParent="true"></TextView>

    <Button
        android:id="@+id/btnRetry"
        android:layout_width="213dp"
        android:layout_height="49dp"
        android:text="Try again"
        android:layout_below="@id/text2"
        android:layout_centerInParent="true"></Button>


</RelativeLayout>

主要活動:

   @Override
   protected void onStart() {
       IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
       registerReceiver(networkChangeListener,intentFilter);
       super.onStart();
       recreate();
   }

   @Override
   protected void onStop() {
       unregisterReceiver(networkChangeListener);
       super.onStop();
   }

將 800dp/1000dp 的 minWidth 和 minHeight 設置為對話框布局文件的 RelativeLayout。

<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"
    android:background="#f7f7f7"
    android:id="@+id/nointernet"
    minWidth="1000dp"
    minHeight="1000dp">
       // rest of the views.
    </RelativeLayout>

我添加了這個並且它工作正常..

<style>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="backgroundColor">#f7f7f7</item>
<item name="android:windowBackground">#f7f7f7</item>
</style>

在我重新創建活動后,按鈕代碼將重新加載網頁:

btnRetry.setOnClickListener(view1 -> {
  if (Common.isConnectedToInternet(context)) {
     dialog.dismiss();
     onReceive(context,intent);
     ((MainActivity)context).recreate();
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM