简体   繁体   中英

Android: How to display transparent loading layer above the activity

I have tested 2 methods to display a transparent loading layer (progress bar) above the activity but activity content gets hidden here's the first one:

<RelativeLayout
  android:id="@+id/loadingPanel"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center" >

  <ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="true" />
</RelativeLayout>

And the other method with style

<RelativeLayout
  style="@style/GenericProgressBackground"
  android:id="@+id/loadingPanel">
  <ProgressBar
    style="@style/GenericProgressIndicator"/>
</RelativeLayout>

<style name="GenericProgressBackground" parent="android:Theme">
  <item name="android:layout_width">fill_parent</item>
  <item name="android:layout_height">fill_parent</item>
  <item name="android:background">#DD111111</item>
  <item name="android:gravity">center</item>
</style>
<style name="GenericProgressIndicator"  arent="@android:style/Widget.ProgressBar.Small">
  <item name="android:layout_width">wrap_content</item>
  <item name="android:layout_height">wrap_content</item>
  <item name="android:indeterminate">true</item>
</style>

And hide or display it

findViewById(R.id.loadingPanel).setVisibility(View.GONE);

(Both are added as the first item inside the root view)

But both methods hide the activity and I want to be visible semi-transparent like the image below how can I do that?

在此处输入图片说明

Like this,

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <!-- Your lay out code here-->

</LinearLayout>

 <RelativeLayout
    android:id="@+id/loadingPanel"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0.8"
        android:background="#000000" />

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:indeterminate="true" />


</RelativeLayout>

</FrameLayout>

Thank me later

  1. create style LoadingDialogTheme

     <style name="LoadingDialogTheme" parent="Widget.AppCompat.ProgressBar"> <item name="background">@android:color/transparent</item> <item name="android:windowBackground">@android:color/transparent</item> </style>
  2. create color:

<color name="dark_overlay">#B3000000</color>
  1. create layout layout_dialog.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_height="match_parent"
       android:background="@color/dark_overlay"
       android:id="@+id/loading_container">
   
       <ProgressBar
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerInParent="true"/>
   
   </RelativeLayout>
  1. extend Dialog LoadingDialog
    open class LoadingDialog(context: Context) : Dialog(context, R.style.LoadingDialogTheme) {
        private val mContext: Context = context
    
         override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            requestWindowFeature(Window.FEATURE_NO_TITLE)
            val inflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val inflateView: View = inflater.inflate(R.layout.loading_dialog, findViewById(R.id.loading_container))
             setCancelable(false)
            setContentView(inflateView)
        }
    
    }
  1. use it
val loader = LoadingDialog(requireContext()) // <-- context
loader.show()

Sorry i forgot to add class.

you can try this,

public class LoadingDialog extends Dialog {
    private Context mContext;

    public LoadingDialog(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View inflateView = inflater.inflate(R.layout.loading_dialog, (ViewGroup) findViewById(R.id.loading_cont));
        setContentView(inflateView);
    }

}

add this layout loading_dialog :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/loading_cont"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RelativeLayout
        android:id="@+id/loading_dialog_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00000000" >

        <ProgressBar
            android:id="@+id/login_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true" >
        </ProgressBar>

    </RelativeLayout>

</LinearLayout>

after that add in your class

LoadingDialog loadingDialog = new LoadingDialog(this);
loadingDialog.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM