【问题标题】:progressbar in the middle of a button按钮中间的进度条
【发布时间】:2016-08-03 13:30:59
【问题描述】:

我想在标准按钮的中间创建一个进度条,如下所示:

在 xml 布局中,当我单击进度条时,我确实看到它位于我希望的位置,但在实时时,我看不到它,感觉就像它隐藏在按钮下方。

我试过了:

  1. 更改视图的位置(进度条上方/下方的按钮)
  2. 不确定
  3. 玩转 ProgressBar 的风格

似乎没有任何效果。

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:elevation="1dp"
        android:text="test button" />

    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button_id"
        android:layout_alignTop="@+id/button_id"
        android:layout_centerHorizontal="true"
        android:indeterminate="true"
        android:indeterminateTint="@android:color/holo_blue_dark"
        android:visibility="visible" />

</RelativeLayout>

【问题讨论】:

  • 可能是因为你需要改变进度条的颜色
  • 以前试过不同的颜色,不是这样。
  • 将这些添加到您的主题基础样式#ffff4444#ff0099cc 再试一次
  • 只是一个用户意识的评论:这看起来很糟糕。至少隐藏文本。
  • @m0skit0 显然......这只是一个例子,在一些随机绘画程序上制作

标签: android android-layout android-xml android-button android-progressbar


【解决方案1】:

问题与elevation 有关。将elevation 添加到进度条

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:elevation="1dp"
        android:text="test button" />

    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button_id"
        android:layout_alignTop="@+id/button_id"
        android:layout_centerHorizontal="true"
        android:indeterminate="true"
        android:elevation="2dp"
        android:indeterminateTint="@android:color/holo_blue_dark"
        android:visibility="visible" />

</RelativeLayout>

【讨论】:

  • 完美。忘记了。只需将高度添加到进度条就可以了。甚至认为提升仅来自 api 21。
  • 为什么是海拔问题?高程解决问题。谢谢
【解决方案2】:

在预览中,ProgressBar 始终是不可见的(至少对我而言)。

问题是,您对按钮应用了高程。这会改变视图的 z 水平。所以 ProgressBar 确实在您的按钮后面,因为 ProgressBar 没有高度。

为 ProgressBar 提供 3dp 的高度对我有用。

<ProgressBar
    android:id="@+id/progress"
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button_id"
    android:layout_alignTop="@+id/button_id"
    android:layout_centerHorizontal="true"
    android:indeterminate="true"
    android:elevation="3dp"
    android:indeterminateTint="@android:color/holo_blue_dark"
    android:visibility="visible" />

编辑: 你会更好,使用ProgressDialog

ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Wait...");
dialog.show();

【讨论】:

  • 如果我只能在 xml 中优雅地做到这一点,我为什么要使用进度对话框?
  • 因为我觉得它不是很“优雅”。将 ProgressBar 设置为可见/不可见的代码并不比显示 ProgressDialog 少很多。
  • ProgressDialog id 已从 api 26 弃用,建议使用 ProgressBar
【解决方案3】:

如果您仍需要支持 21 岁以上的设备(因此无法使用 android:elevation),请尝试将您的按钮包装在另一个虚拟布局中。我以FrameLayout 为例。

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

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

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Submit" />
    </FrameLayout>

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="24dp"
        android:layout_gravity="center"
        android:indeterminateTint="@android:color/white" />
</FrameLayout>

【讨论】:

    猜你喜欢
    • 2011-06-14
    • 2018-02-20
    • 2021-05-29
    • 2019-01-29
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多