【问题标题】:Positioning a progress bar behind a button在按钮后面放置进度条
【发布时间】:2012-05-29 13:32:21
【问题描述】:

我有一个Button,它在RelativeLayout 中定义如下:

<Button
        android:id="@+id/search_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/current_location"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:paddingBottom="30dp"
        android:paddingTop="30dp"
        android:text="@string/search" />

我想在Button后面加一个ProgressBar,和Button占据完全相同的空间,这样当按钮被禁用(半透明)时,你可以看到它后面的进度条。我该怎么做?

【问题讨论】:

    标签: android layout button progress-bar android-relativelayout


    【解决方案1】:

    为此,我必须先在main.xml 中定义按钮,然后是进度条,如下所示:

    <Button
        android:id="@+id/search_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/current_location"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:paddingBottom="30dp"
        android:paddingTop="30dp"
        android:text="@string/search" />
    
    <ProgressBar
        android:id="@+id/cooldown_bar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/search_button"
        android:layout_below="@id/current_location"
        android:layout_marginBottom="6dp"
        android:layout_marginLeft="9dp"
        android:layout_marginRight="9dp"
        android:layout_marginTop="1dp"
        android:indeterminate="false"
        android:progressDrawable="@drawable/progress_bar" />
    

    然后,一旦我初始化了所有的 GUI 组件,我必须将按钮带回到进度条的前面:

    searchButton.bringToFront();
    

    最终结果

    启用时的按钮:

    使用进度条禁用时的按钮:

    【讨论】:

      【解决方案2】:

      把它放在 xml 中相对布局内的按钮之前:

          <ProgressBar
          android:id="@+id/descriptive_name"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/current_location"
          android:layout_marginLeft="5dp"
          android:layout_marginRight="5dp"
          android:paddingBottom="30dp"
          android:paddingTop="30dp"
           />
      

      通过使用所有相同的layotu和padding属性,我们可以确保视图在同一个位置并且大小大致相同(由于wrap_content,高度可能不同)我想如果你把进度条放在前面布局文件,然后按钮应该出现在顶部。

      但您也可以使用view.setVisibility(visibility-ness)执行您想要的任何策略,只要确保无论何时让一个可见,您就可以让另一个不可见。

      这样,如果我对它们在文件中的位置(我可能是,我刚刚没有验证它)以及你不想移动的情况下默认情况下的堆叠方式有误文件中按钮下方的进度条,从用户的角度来看,您仍然可以实现相同的效果。

      【讨论】:

      • 即把进度条放在按钮后面,但是进度条的宽度大于按钮的宽度。此外,进度条高度要小很多。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-16
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多