Android-无法将progressBar移到中心

时间:2019-05-30 07:49:15

标签: android kotlin layout progress-bar

与Kotlin一起工作时,我想在BaseActivity中实施showProgressBar

abstract class BaseActivity : AppCompatActivity() {

        var progressBar: ProgressBar? = null

        fun showProgressBar() {
            progressBar = ProgressBar(this, null, android.R.attr.progressBarStyleSmall)
            val params = RelativeLayout.LayoutParams(120, 120)
            params.addRule(RelativeLayout.CENTER_IN_PARENT)
            val layout = this.findViewById<ViewGroup>(android.R.id.content)
            layout.addView(progressBar, params)
        }

        fun hideProgressBar() {

           progressBar?.visibility = GONE
        }
}

MainActivity布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MainActivity">

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_centerInParent="true" />

</RelativeLayout>

我从MainActivity拨打电话,

class MainActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Log.i(TAG, "calling showProgressBar")
        showProgressBar()

        Handler(Looper.getMainLooper()).postDelayed(Runnable {

            Log.i(TAG, "calling hideProgressBar")
            hideProgressBar()

        }, 5000)
    }
}

但是params.addRule(RelativeLayout.CENTER_IN_PARENT)似乎不起作用。输出在下面的屏幕截图中。如何在屏幕中央移动progressBar?

enter image description here

0 个答案:

没有答案