如何在 Android 中制作进度条触摸底部屏幕边框
How to make an progressBar touch bottom screen border in Android
我遇到了与 here
中提到的类似问题
这是我的 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true">
<ImageButton
android:id="@+id/imageButton”
android:layout_width="40dip"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:contentDescription="@string/descrp"
android:scaleType="center"
android:src="@drawable/some_drawable”
android:visibility="visible" />
<TextView
android:id="@+id/textview”
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<ProgressBar
android:id="@+id/progressbar”
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
如何让 ProgressBar 触摸屏幕的边框,并且进度条和边框之间没有任何间隙?
我找到了一个作品 around.I 我基本上是在我的 ProgressBar 中添加一个 layout_marginBottom="-4dp" 并将它包裹在一个 RelativeLayout 中并将它对齐到父元素的底部 view.This 更改可能会破坏您的应用 future.For 更好的解决方案 使用您自己的自定义可绘制对象设计自定义进度条,与 progressBarStyleHorizontal 相比,您可以正确对齐并占用更少 canvas space。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true">
<ImageButton
android:id="@+id/imageButton”
android:layout_width="40dip"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:contentDescription="@string/descrp"
android:scaleType="center"
android:src="@drawable/some_drawable”
android:visibility="visible" />
<TextView
android:id="@+id/textview”
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textColor="#FFFFFF"
android:textStyle="bold" />
//changes
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/some_other_id"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-4dp" />
</RelativeLayout></RelativeLayout>
我已经为此苦苦挣扎了一段时间。我的结论:对于任意屏幕尺寸,无法通过 with XML 实现此行为。在某些屏幕分辨率下,进度条总是会错位。
我的简单解决方案:将 progressBar 的 Y 以编程方式设置为超级 view/layout 的底部减去 progressBar 高度的一半
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
progressBar.setY(layout.getY() + layout.getHeight() - progressBar.getHeight() / 2);
}
非常适合所有屏幕尺寸。
只需要在 ProgressBar 中添加以下属性即可
android:minHeight="4dp" // or whatever height you want
android:maxHeight="4dp" // or whatever height you want
完整代码:
<ProgressBar
android:id="@+id/snackbar_progress_crop"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:indeterminateOnly="false"
android:minHeight="4dp"
android:maxHeight="4dp"
/>
我遇到了与 here
中提到的类似问题这是我的 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true">
<ImageButton
android:id="@+id/imageButton”
android:layout_width="40dip"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:contentDescription="@string/descrp"
android:scaleType="center"
android:src="@drawable/some_drawable”
android:visibility="visible" />
<TextView
android:id="@+id/textview”
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<ProgressBar
android:id="@+id/progressbar”
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
如何让 ProgressBar 触摸屏幕的边框,并且进度条和边框之间没有任何间隙?
我找到了一个作品 around.I 我基本上是在我的 ProgressBar 中添加一个 layout_marginBottom="-4dp" 并将它包裹在一个 RelativeLayout 中并将它对齐到父元素的底部 view.This 更改可能会破坏您的应用 future.For 更好的解决方案 使用您自己的自定义可绘制对象设计自定义进度条,与 progressBarStyleHorizontal 相比,您可以正确对齐并占用更少 canvas space。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true">
<ImageButton
android:id="@+id/imageButton”
android:layout_width="40dip"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:contentDescription="@string/descrp"
android:scaleType="center"
android:src="@drawable/some_drawable”
android:visibility="visible" />
<TextView
android:id="@+id/textview”
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textColor="#FFFFFF"
android:textStyle="bold" />
//changes
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/some_other_id"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-4dp" />
</RelativeLayout></RelativeLayout>
我已经为此苦苦挣扎了一段时间。我的结论:对于任意屏幕尺寸,无法通过 with XML 实现此行为。在某些屏幕分辨率下,进度条总是会错位。
我的简单解决方案:将 progressBar 的 Y 以编程方式设置为超级 view/layout 的底部减去 progressBar 高度的一半
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
progressBar.setY(layout.getY() + layout.getHeight() - progressBar.getHeight() / 2);
}
非常适合所有屏幕尺寸。
只需要在 ProgressBar 中添加以下属性即可
android:minHeight="4dp" // or whatever height you want
android:maxHeight="4dp" // or whatever height you want
完整代码:
<ProgressBar
android:id="@+id/snackbar_progress_crop"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:indeterminateOnly="false"
android:minHeight="4dp"
android:maxHeight="4dp"
/>