默认情况下,ProgressBar 在其本身上下有一定的填充。是否有一种方法可以去除这个填充,只保留最终的进度条?
默认情况下,ProgressBar 在其本身上下有一定的填充。是否有一种方法可以去除这个填充,只保留最终的进度条?
在Java文件中设置所需的高度值,最重要的是设置setMaxHeight。
progressBar.setMinHeight(heightYouWant);
progressBar.setMaxHeight(heightYouWant);
这对我有用!
<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"/>
<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" />
View
在LinearLayout
中伪造ProgressBar
。这就是我最终得出的结果。看起来非常整洁,这种方法相当灵活-您可以以时髦的方式对其进行动画处理,添加文本等。<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>
代码:
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);
}
结果(添加了一些高度):
将进度条向上移动的一种简单方法是使用属性android:translationZ
,它不需要任何额外的视图或边距调整。
这样你可以在XML中使用它。
<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>
最好的解决方案应该是
android:minHeight="0dp"
没有任何变通方法,而且工作得非常好。
minHeight
比0大(可能是32dp)预定义。通过更改此属性,您可以获得不同的外观。 - Zhen Fan style="@android:style/Widget.ProgressBar.Horizontal"
android:progressTint="Your Progress Color"
android:progressTintMode="src_over"
android:progressBackgroundTint="Your Background Color"
android:backgroundTintMode="src_over"
这个功能是通过在 Widget.ProgressBar.Horizontal
中呈现的进度或背景颜色上进行着色来实现的。
maxHeight
无效。 - abergmeierandroid:minHeight="4dp"
- Sam Chen