Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从水平进度条上拆下垂直填充_Android_Android Progressbar - Fatal编程技术网

Android 从水平进度条上拆下垂直填充

Android 从水平进度条上拆下垂直填充,android,android-progressbar,Android,Android Progressbar,默认情况下,ProgressBar在其上方和下方都有特定的填充。是否有办法删除此填充,以便只在最后添加条?我使用以下方法解决此问题 android:layout_marginBottom="-8dp" android:layout_marginTop="-4dp" 这个问题的完整解决方案如下。以防有人需要代码片段,我就是这么做的 复制了所有8个不确定的水平progressbar绘图项 使用一些图像操纵器编辑可绘制内容并删除不必要的填充 从android平台复制了名为progress_Undet

默认情况下,ProgressBar在其上方和下方都有特定的填充。是否有办法删除此填充,以便只在最后添加条?

我使用以下方法解决此问题

android:layout_marginBottom="-8dp"
android:layout_marginTop="-4dp"

这个问题的完整解决方案如下。以防有人需要代码片段,我就是这么做的

  • 复制了所有8个不确定的水平progressbar绘图项
  • 使用一些图像操纵器编辑可绘制内容并删除不必要的填充
  • 从android平台复制了名为progress_Undeterminate_horizontal_holo.XML的可绘制XML
  • 复制了样式小部件.ProgressBar.Horizontal及其父项
  • 在布局中手动设置样式和最小高度
  • 下面是progress\u undeterminate\u horizontal\u holo.xml

    <animation-list
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="false">
        <item android:drawable="@drawable/progressbar_indeterminate_holo1" android:duration="50" />
        <item android:drawable="@drawable/progressbar_indeterminate_holo2" android:duration="50" />
        <item android:drawable="@drawable/progressbar_indeterminate_holo3" android:duration="50" />
        <item android:drawable="@drawable/progressbar_indeterminate_holo4" android:duration="50" />
        <item android:drawable="@drawable/progressbar_indeterminate_holo5" android:duration="50" />
        <item android:drawable="@drawable/progressbar_indeterminate_holo6" android:duration="50" />
        <item android:drawable="@drawable/progressbar_indeterminate_holo7" android:duration="50" />
        <item android:drawable="@drawable/progressbar_indeterminate_holo8" android:duration="50" />
    </animation-list>
    
    
    
    样式资源已复制到本地样式文件

    <style name="Widget">
        <item name="android:textAppearance">@android:attr/textAppearance</item>
    </style>
    
    <style name="Widget.ProgressBar">
        <item name="android:indeterminateOnly">true</item>
        <item name="android:indeterminateBehavior">repeat</item>
        <item name="android:indeterminateDuration">3500</item>
    </style>
    
    <style name="Widget.ProgressBar.Horizontal">
        <item name="android:indeterminateOnly">false</item>
        <item name="android:indeterminateDrawable">@drawable/progress_indeterminate_horizontal_holo</item>
    </style>
    
    
    @android:attr/textpearance
    真的
    重复
    3500
    假的
    @可绘制/进度\不确定\水平\全息图
    
    最后,在本地布局文件中将“最小高度”设置为4dp

    <ProgressBar
        android:id="@+id/pb_loading"
        style="@style/Widget.ProgressBar.Horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:indeterminate="true"
        android:minHeight="4dp"
        android:minWidth="48dp"
        android:progressDrawable="@drawable/progress_indeterminate_horizontal_holo" />
    

    可以在父级内部绘制垂直居中的进度条,以去除填充。由于ProgressBar不能将自身绘制得比父视图大,因此必须创建一个大的父视图以放置在剪裁视图中

    <FrameLayout
        android:id="@+id/clippedProgressBar"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        tools:ignore="UselessParent">
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="16dp"
            android:layout_gravity="center_vertical">
    
            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:indeterminate="true"/>
    
        </FrameLayout>
    
    </FrameLayout>
    

    以下是我如何使用Juozas的答案:

    my
    ProgressBar
    的高度为4dp。因此,我创建了一个高度为4dp的
    FrameLayout
    ,并将
    ProgressBar的
    layout\u gravity
    设置为
    center
    。这是一种魅力

    
    

    注意:框架布局
    的作用是去除多余的部分,因此如果您遇到
    进度条
    仍然很薄的问题,只需将
    进度条
    布局高度
    设置为一些较大的数字,如
    100dp
    。它将完全覆盖框架布局,并且只显示其中的
    4dp

    我使用minHeight和maxHeigh。它有助于不同的Api版本

    <ProgressBar
        android:id="@+id/progress_bar"
        style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxHeight="3dp"
        android:minHeight="3dp" />
    

    但在这种情况下,较低的Api版本会将进度条的高度增加到最大高度。

    Subin的答案似乎是(目前)唯一一个在Android进度条未来版本中不会被破坏的脆弱黑客

    但是,我选择了使用,这为我们做到了这一点,而不是麻烦地开发、修改和无限期地维护资源:

    <me.zhanghai.android.materialprogressbar.MaterialProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:layout_gravity="bottom"
        custom:mpb_progressStyle="horizontal"
        custom:mpb_showTrack="false"
        custom:mpb_useIntrinsicPadding="false"
        style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal.NoPadding"
    />
    

    该项目有一个显示它与内置ProgressBar之间差异的图标。

    将android:progressDrawable添加到drawable中定义的层列表中修复了我的问题。它的工作原理是在自定义的可绘制图形中屏蔽进度条


    中描述的示例实现最终使用自定义库来解决此问题。大多数其他解决方案都能工作,但结果在不同的设备上并不一致

    材料程序条

    • Android 4.0+上的一致外观
    • 跨平台正确着色
    • 能够删除framework ProgressBar的内部填充
    • 能够隐藏框架水平进度条的轨迹
    • 用作framework ProgressBar的插入式替换
    要添加为渐变依赖项,请执行以下操作:

    compile 'me.zhanghai.android.materialprogressbar:library:1.1.7'
    
    要在布局中添加无内部填充的ProgressBar,请执行以下操作:

    <me.zhanghai.android.materialprogressbar.MaterialProgressBar
        android:layout_width="wrap_content"
        android:layout_height="4dp"
        android:indeterminate="true"
        app:mpb_progressStyle="horizontal"
        app:mpb_useIntrinsicPadding="false"
        style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal" />
    
    
    

    app:mpb\u useIntrinsicPadding=“false”
    起作用。有关更多详细信息,请参阅。

    我在使用水平样式的progressbar时遇到了相同的问题

    根本原因是默认的9-patch可用于进度条绘制: (progress_bg_holo_dark.9.png)有一些垂直透明像素作为填充

    对我有效的最终解决方案是:定制可绘制的进度,我的示例代码如下:

    自定义\u水平\u进度条\u drawable.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@android:id/background">
            <shape android:shape="rectangle">
                <solid android:color="#33ffffff" />
            </shape>
        </item>
        <item android:id="@android:id/secondaryProgress">
            <clip>
                <shape android:shape="rectangle">
                    <solid android:color="#ff9800" />
                </shape>
            </clip>
        </item>
        <item android:id="@android:id/progress">
            <clip>
                <shape android:shape="rectangle">
                    <solid android:color="#E91E63" />
                </shape>
            </clip>
        </item>
    </layer-list>
    
    
    
    版面片段:

    <ProgressBar
        android:id="@+id/song_progress_normal"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:layout_alignParentBottom="true"
        android:progressDrawable="@drawable/custom_horizontal_progressbar_drawable"
        android:progress="0"/>
    
    
    
    我使用的是
    style=“@style/Widget.AppCompat.ProgressBar.Horizontal”
    ,很容易去掉边距。这种风格是:

        <item name="progressDrawable">@drawable/progress_horizontal_material</item>
        <item name="indeterminateDrawable">@drawable/progress_indeterminate_horizontal_material</item>
        <item name="minHeight">16dip</item>
        <item name="maxHeight">16dip</item>
    
    @可拉伸/进度\u水平\u材料
    @可拉伸/进度\不确定\水平\材料
    16倾角
    16倾角
    
    我刚刚超越了最小/最大高度:

        <ProgressBar
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:indeterminate="true"
            android:minHeight="2dp"
            android:maxHeight="2dp" />
    
    
    
    尝试以下操作:

    <ProgressBar
        android:id="@+id/progress_bar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:progress="25"
        android:progressTint="@color/colorWhite"
        android:progressBackgroundTint="@color/colorPrimaryLight"
        android:layout_width="match_parent"
        android:layout_height="4dp" />
    
    
    

    。。。然后根据您的需要配置进度条,因为它最初会显示一个中等大小的进度条,带有黄色进度色调和灰色进度背景色调。另外,请注意,没有垂直填充。

    一个简单的无技巧解决方案,它与任何版本的Android兼容,并且不需要外部库,它使用两个
    视图假装
    进度条
    ,内部
    线性布局
    。这就是我的结局。看起来很整洁,这种方法非常灵活——你可以用时髦的方式制作动画,添加文本等等

    布局:

    <LinearLayout
        android:id="@+id/inventory_progress_layout"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/inventory_progress_value"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <TextView
            android:id="@+id/inventory_progress_remaining"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    
    结果(添加了一些高程):


    一个技巧是在进度条中添加负边距

    下面是一个XML代码示例,假设它位于屏幕顶部:

    <ProgressBar
        android:id="@+id/progressBar"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="-7dp"
        android:layout_marginBottom="-7dp"
        android:indeterminate="true" />
    
    
    

    如果有人仍然需要帮助,可以尝试以下方法:

    
    
    在这里,进度条位于ConstraintLayout内,constraintTop_toTopOf和constraintBottom_toTopOf属性必须应用于同一元素(在本例中,它是一个指南)

    ***完整解决方案:**

    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="48dp">
    
        <View
            android:id="@+id/guideline"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="vertical"
            android:visibility="invisible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    
        <ProgressBar
            android:id="@+id/progress_bar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:indeterminate="true"
            app:layout_constraintBottom_toTopOf="@+id/guideline"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    要删除
    ProgressBar的垂直填充,您可以通过

    • 固定
      ProgressBar的高度
    • 使用s
      public void setProgressValue(float percentage) {
          TextView progressValue = (TextView) findViewById(R.id.inventory_progress_value);
          TextView progressRemaining = (TextView) findViewById(R.id.inventory_progress_remaining);
      
          LinearLayout.LayoutParams paramsValue = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
          LinearLayout.LayoutParams paramsRemaining = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
      
          paramsValue.weight = (100 - percentage);
          paramsRemaining.weight = percentage;
      
          progressValue.setLayoutParams(paramsValue);
          progressRemaining.setLayoutParams(paramsRemaining);
      
      }
      
      <ProgressBar
          android:id="@+id/progressBar"
          style="@style/Widget.AppCompat.ProgressBar.Horizontal"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="-7dp"
          android:layout_marginBottom="-7dp"
          android:indeterminate="true" />
      
      <androidx.constraintlayout.widget.ConstraintLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="48dp">
      
          <View
              android:id="@+id/guideline"
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:orientation="vertical"
              android:visibility="invisible"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent" />
      
          <ProgressBar
              android:id="@+id/progress_bar"
              style="?android:attr/progressBarStyleHorizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:indeterminate="true"
              app:layout_constraintBottom_toTopOf="@+id/guideline"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="@+id/guideline" />
      </androidx.constraintlayout.widget.ConstraintLayout>
      
      <ProgressBar
          style="?android:attr/progressBarStyleHorizontal"
          ...
          android:layout_height="8dp"
          android:scaleY="2" />
      
       <ProgressBar
              android:layout_marginTop="-8dp"
              android:layout_marginLeft="-8dp"
              android:layout_marginRight="-8dp"
              android:id="@+id/progress_bar"
              style="?android:attr/progressBarStyleHorizontal"
              android:layout_width="match_parent"
              android:layout_height="4dp"
              android:indeterminate="false"
              android:indeterminateTint="@color/white"
              android:max="100"
              android:paddingStart="8dp"
              android:paddingRight="0dp"
              android:progressDrawable="@drawable/progress_bg" />
      
      <ProgressBar
          android:id="@+id/workoutSessionGlobalProgress"
          android:layout_width="match_parent"
          android:layout_height="YOUR_HEIGHT"
          android:progressDrawable="@drawable/progress_horizontal"
          android:progress="0"
          <!-- High value to make ValueAnimator smoother -->
          android:max="DURATION * 1000"
          android:indeterminate="false"
          style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal"/>
      
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      
          <item android:id="@android:id/background">
              <shape>
                  <corners android:radius="5dip" />
                  <gradient
                          android:startColor="#ff9d9e9d"
                          android:centerColor="#ff5a5d5a"
                          android:centerY="0.75"
                          android:endColor="#ff747674"
                          android:angle="270"
                          />
              </shape>
          </item>
      
          <item android:id="@android:id/secondaryProgress">
              <clip>
                  <shape>
                      <corners android:radius="5dip" />
                      <gradient
                              android:startColor="#80ffd300"
                              android:centerColor="#80ffb600"
                              android:centerY="0.75"
                              android:endColor="#a0ffcb00"
                              android:angle="270"
                              />
                  </shape>
              </clip>
          </item>
      
          <item android:id="@android:id/progress">
              <clip>
                  <shape>
                      <corners android:radius="5dip" />
                      <gradient
                              android:startColor="#ffffd300"
                              android:centerColor="#ffffb600"
                              android:centerY="0.75"
                              android:endColor="#ffffcb00"
                              android:angle="270"
                              />
                  </shape>
              </clip>
          </item>
      
      </layer-list>
      
      android:minHeight="0dp"
      
       <LinearLayout
           android:layout_width="match_parent"
           android:background="#efefef"
           android:layout_height="wrap_content">
      
           <ProgressBar
               android:id="@+id/progressBar"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:indeterminate="true"
               android:visibility="gone"
               android:layout_marginTop="-7dp"
               android:layout_marginBottom="-7dp"
               style="@style/Widget.AppCompat.ProgressBar.Horizontal" />
       </LinearLayout>
      
         android:minHeight="4dp"
      
        <ProgressBar
          android:id="@+id/web_view_progress_bar"
          style="@style/Widget.AppCompat.ProgressBar.Horizontal"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:indeterminate="false"
          android:max="100"
          android:min="0"
          android:progress="5"
          android:minHeight="4dp"
          android:progressTint="@color/vodafone_red"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          tools:progress="60" />
      
      <androidx.core.widget.ContentLoadingProgressBar
                          android:id="@+id/progressBar"
                          style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                          android:layout_width="0dp"
                          android:layout_height="wrap_content"
                          android:indeterminate="true"
                          android:paddingTop="2dp"
                          app:layout_constraintTop_toTopOf="parent"
                          app:layout_constraintBottom_toTopOf="parent"
                          app:layout_constraintEnd_toEndOf="parent"
                          app:layout_constraintStart_toStartOf="parent"/>
      
      <com.google.android.material.progressindicator.ProgressIndicator
              android:id="@+id/progressBar"
              style="@style/Widget.MaterialComponents.ProgressIndicator.Linear.Indeterminate"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:indicatorColor="@color/colorPrimary"
              app:trackColor="@color/colorAccent" />
      
      <com.google.android.material.progressindicator.LinearProgressIndicator
          android:id="@+id/progress_loading"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:visibility="visible"
          android:indeterminate="true"
          app:indicatorColor="@color/colorAccent"
          app:layout_constraintTop_toBottomOf="@+id/app_bar_pdfview"/>
      
      implementation 'com.google.android.material:material:1.3.0'
      
      <Progressbar
       android:layout_height="wrap_content"
       android:layout_width="match_parent"
       android:translateY="-6dp"
      />
      
      <style name="MyTheme.Progressbar.Horizontal" parent="Widget.AppCompat.Progressbar.Horizontal">
          <item name="android:translateY">-6dp</item>
      </style>
      
      <Progressbar
       android:layout_height="wrap_content"
       android:layout_width="match_parent"
       style="@style/MyTheme.Progressbar.Horizontal"
       />
      
      <FrameLayout
          android:layout_width="match_parent"
          android:layout_height="10dp">
      
          <ProgressBar
              android:id="@+id/progress_bar"
              style="?android:attr/progressBarStyleHorizontal"
              android:layout_width="match_parent"
              android:layout_height="100dp"
              android:layout_gravity="center"
              android:backgroundTint="@android:color/transparent"
              android:indeterminate="true"
              android:indeterminateTint="@color/white" />
      
      </FrameLayout>
      
          style="@android:style/Widget.ProgressBar.Horizontal"
          android:progressTint="Your Progress Color"
          android:progressTintMode="src_over"
          android:progressBackgroundTint="Your Background Color"
          android:backgroundTintMode="src_over"