[英]how to show progress bar center of screen
我的代码在列表视图顶部显示空间(见左图)。 如果我删除android:gravity="center"
那么它会正确显示(见右图)
但是 ProgressBar 不显示在屏幕中央,它显示在屏幕左侧。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ListView
android:id="@+id/listCategory2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="true"
android:fastScrollEnabled="true"
android:divider="@color/background"
android:dividerHeight="1dp"
android:visibility="gone"
android:background="@drawable/border9"
android:listSelector="@drawable/listview_selector" />
<ProgressBar
android:id="@+id/prgLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_centerInParent="true" />
<TextView
android:id="@+id/txtAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/alert"
android:textColor="@color/text"
android:textSize="14sp"
android:layout_centerInParent="true"
android:visibility="gone" />
</LinearLayout>
您采用的父布局是 LinearLayout 并且您的 xml android:layout_centerInParent="true" 中的方法适用于 RelativeLayout。 您需要将其更改为 android:layout_gravity="center"。 您可以改为将父布局更改为 RelativeLayout。
你可以使用<FrameLayout>
,它真的对你有好处。
并将进度条( layout_width
和layout_height
需要match_parent
)设置为:
<ProgressBar
android:id="@+id/prgLoading"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_centerInParent="true"/>
希望有所帮助,
使其成为RelativeLayout
而不是LinearLayout
,并为ListView
添加android:layout_alignParentTop="true"
并为进度条添加android:layout_centerInParent="true"
<ProgressBar
android:id="@+id/loading_spinner"
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
/>
对于线性布局
在ConstraintLayout
其居中的简单方法
<androidx.core.widget.ContentLoadingProgressBar
android:id="@+id/myProgress"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.