[Android] to load the prompt box

LoadingFragment

class LoadingFragment : DialogFragment() {

    companion object {

        fun newInstance(): LoadingFragment {
            val fragment = LoadingFragment()
            val args = Bundle()
            fragment.setArguments(args)
            return fragment
        }
    }

    private var mProgressBar: ProgressBar? = null
    private var mProgressTv: TextView? = null
    private var mTipTv: TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, R.style.AppTheme);
        if (getArguments() != null) {
        }
    }

    /**
     * 布局
     * */
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_loading, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mProgressBar = progressBar
        mProgressTv = progressTv
        mTipTv = tipTv
        progressBar.progress = 1
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        // 去除标题栏
        dialog.window?.requestFeature(Window.FEATURE_NO_TITLE);

        super.onActivityCreated(savedInstanceState);
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.WHITE))
        dialog.window?.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT);
        dialog.setCancelable(true);
    }

    /**
     * 设置进度条进度.
     * */
    fun setProgress(progress: Int){
        activity?.runOnUiThread {
            mProgressBar?.progress = progress
            mProgressTv?.text = "$progress%"
        }
    }

    /**
     * 设置提示信息.
     * */
    fun setTips(tips: String) {
        activity?.runOnUiThread {
            mTipTv?.text = tips
        }
    }
}
复制代码

fragment_loading.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
        android:layout_height="match_parent">


    <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:max="100"
            android:progressDrawable="@drawable/bg_progress"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

    <TextView
            android:id="@+id/progressTv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1%"
            android:gravity="center"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@id/progressBar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

    <TextView
            android:id="@+id/tipTv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="加载中"
            android:gravity="center"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/progressBar" />

</androidx.constraintlayout.widget.ConstraintLayout>
复制代码

bg_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="10dp" />

            <gradient
                    android:centerColor="@color/colorPrimary"
                    android:centerY="0.5"
                    android:endColor="@color/colorPrimary"
                    android:startColor="@color/colorPrimary"
                    android:type="linear" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="10dp" />

                <gradient
                        android:centerColor="@color/colorPrimary"
                        android:centerX="1.0"
                        android:endColor="@color/colorPrimary"
                        android:startColor="@color/colorPrimary" />
            </shape>
        </clip>
    </item>

</layer-list>
复制代码

transfer

val fragment = LoadingFragment.newInstance()
fragment?.show(supportFragmentManager, "loding")
fragment?.setTips("下载中...")
fragment?.setProgress(progress)
fragment?.dismiss()
复制代码

Scan code concerned with the exchange of learning ~~

Reproduced in: https: //juejin.im/post/5d0b4853f265da1bb96fe3f0

Guess you like

Origin blog.csdn.net/weixin_33801856/article/details/93180735