Android 5.0 - ProgressBar无法在Button上显示

Mat*_*ree 24 android android-progressbar android-button android-5.0-lollipop

我认为标题非常明确我的问题......所以这是我的布局:

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <Button
                    android:id="@+id/button_action"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Login" />

                <ProgressBar
                    android:id="@+id/progress_bar"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_centerInParent="true"/>

            </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在android SDKs <21上没问题,ProgressBar正确地显示在Button上并且在Button中居中.但是在Android 5.0上,ProgressBar显示 Button 后面.

因此,当您在"开发者选项"设置中激活"显示布局边界"选项时,您可以看到它已正确定位,但如果没有该选项,您将无法在屏幕上看到任何内容.

有人知道如何解决这个问题吗?我想这是一个事海拔最近推出的,但我真的不知道该怎么照顾它.为了记录,我正在使用support.v7中最近发布的Theme.AppCompat样式.

编辑:

我还尝试以编程方式应用setElevation(0)setTranslationY(0)按钮,但它没有改变任何东西.所以我想知道它是否必须处理高程.

先谢谢你们

马蒂厄

wpi*_*ski 56

您可以在ProgressBar中使用android:translationZ属性:

        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:translationZ="2dp"
            android:layout_centerInParent="true"/>
Run Code Online (Sandbox Code Playgroud)

  • 有点奇怪.我已经在模拟器上检查了代码,而ProgressBar现在已经在Button上了.1 dp是不够的,但是2 dp将ProgressBar移到前面. (2认同)

Tob*_*iug 9

同样的问题在这里,我简单的"黑客"也将Button包装到另一个FrameLayout中.这样我就不关心api版本和其他高程问题;)

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" >

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
                <Button
                    android:id="@+id/button_action"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Login" />
        </FrameLayout>

        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="50dp"
            android:layout_height="50dp" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)


Lam*_*gaR 6

在这里提出同样的问题,更好地解释了这个问题:

/sf/answers/1905145791/

引用@CommonsWare:

问题出现在Android 5.0的elevation属性中.显然,RelativeLayoutZ轴排序与之相关elevation.如果两个窗口小部件具有相同的elevation,RelativeLayout则将确定Z轴顺序 - 例如,如果要将布局切换为两个Button窗口小部件,则可以看到.但是,如果一个widget(Button)有一个elevation,而另一个widget(ImageView)没有,则elevation优先.

您可以删除Button elevation通道 android:stateListAnimator="@null"定义自己的自定义动画师.或者,您可以添加一些elevationImageView使其在Z轴上高于Button.