2

我想在一个标准的按钮中间创建一个进度条,这样的事情: progressbar进度在按钮的中间

在XML布局,当我点击的进度,我做看到它位于我希望的位置,但实时,我看不到它,它感觉就像隐藏在按钮下面。 android studio xml

我已经试过:

  1. 更改视图的定位(钮高于/低于进度)
  2. 不确定
  3. 与进度的风格演奏

似乎没有什么是工作。

这里是我的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> 
+0

也许是因为你需要前更改进度条 – has19

+0

的颜色尝试了不同的颜色,它不是。 – Dus

+0

将这些添加到您的主题库样式中#ffff4444 #ff0099cc并再试一次 – has19

回答

6

该问题与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> 
+1

完美。忘了那个。只需在进度条中添加高程就可以实现。甚至认为海拔仅来自api 21. – Dus

+1

为什么海拔问题?立面解决问题。谢谢 –

0

在预览中,进度始终是无形的(至少对我来说)。

问题是,您将高程应用于按钮。这会更改视图的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(); 
+0

为什么我应该使用progressdialog,如果我只能在xml中优雅地做到这一点,而没有任何代码背后呢? – Dus

+0

,因为我觉得它“非常优雅”。将ProgressBar设置为可见/不可见不会比代表ProgressDialog的代码少得多。 – AlbAtNf