从 API 级别 21 开始,默认Button
(在 Material Design 中称为凸起按钮)具有静止高度和按下高度。
例如,在 API 级别 23 中,值分别为 2dp 和 6dp。
Your ProgressBar
位于正确的位置,但低于Button
因为它的海拔是0。
因此,只需将大于 6dp 的高度添加到您的ProgressBar
你可以让它出现在按钮上方。
android:elevation="7dp"
或者,您可以创建一个布局来模仿按钮并为其指定按钮样式:
<LinearLayout
style="@style/Widget.AppCompat.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Button"
android:textAppearance="@style/TextAppearance.AppCompat.Button"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
第二种方法更具前向兼容性,因为海拔可能会在未来版本中发生变化。
例如,在 API 级别 21 中,值为 1dp 和 3dp
<!-- Elevation when button is pressed -->
<dimen name="button_elevation_material">1dp</dimen>
<!-- Z translation to apply when button is pressed -->
<dimen name="button_pressed_z_material">2dp</dimen>
这是 API 级别 23
<!-- Elevation when button is pressed -->
<dimen name="button_elevation_material">2dp</dimen>
<!-- Z translation to apply when button is pressed -->
<dimen name="button_pressed_z_material">4dp</dimen>