【问题标题】:Android - How to make an progressBar touching a bottom screen borderAndroid - 如何使progressBar触摸底部屏幕边框
【发布时间】:2014-03-14 20:06:24
【问题描述】:

我想要一个正在触摸屏幕底部边框的 ProgressBar,但是通过这段代码,我在栏和屏幕边框之间得到了一点空间:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".Whatever" >

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" />

</RelativeLayout>

这就是我得到的:

有什么办法可以去掉那个空格?

【问题讨论】:

  • 您是否尝试将边距底部设置为零?
  • 我有,它没有帮助。
  • 嗨大卫,你解决了这个问题吗?
  • 嗨,我没有,因为我从那个项目搬走了。
  • “感谢回复。”是正确的(阅读帮助->游览)以及不正确的英语,所以下次不要闲聊。

标签: android android-progressbar


【解决方案1】:

将您的父布局高度设置为 fill_parent 并将进度条高度设置为 match_parent。

【讨论】:

    【解决方案2】:

    我想出了使用自定义 progressDrawable 的解决方案,没有像这样的任何填充:

    progress_bar_progress.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="@color/accent"/>
            </shape>
        </clip>
    </item>
    

    然后在您的ProgressBar 中引用它:

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_alignParentBottom="true"
        android:background="#1A000000"
        style="?android:attr/progressBarStyleHorizontal"
        android:indeterminateOnly="false"
        android:progressDrawable="@drawable/progress_bar_progress" />
    

    【讨论】:

      【解决方案3】:

      这是一个可行的技巧:

      • 为整个视图创建一个 LinearLayout;将宽度和高度设置为 fill_parent 并将方向设置为垂直
      • 现在,既然您知道有多少组件会出现在您的视野中,只需给它们适当的权重即可;除了最后一个进度条
      • 对于进度条,将其设为最小的;这样,它就会一直被推到底;
      • 最后,使用填充来减少它与窗口之间的空间(底部)

      这就是我能想到的,除非您想为上面评论中提到的进度条创建自定义样式

      【讨论】:

        【解决方案4】:

        我找到了一个解决方法。我基本上是在我的 ProgressBar 中添加一个 layout_marginBottom="-4dp" 并将其包装在一个 RelativeLayout 中,并将其与父视图的底部对齐。这些更改将来可能会破坏您的应用程序。为了获得更好的解决方案,请设计一个带有您自己的自定义可绘制对象的自定义进度条,与 progressBarStyleHorizo​​ntal 相比,您可以正确对齐并占用更少的画布空间。

        <?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>
        

        这是link

        【讨论】:

          【解决方案5】:

          我已经为此苦苦挣扎了一段时间。我的结论: 对于任意屏幕尺寸,使用 XML 无法归档此行为。 在某些屏幕分辨率上,进度条总是会错位。

          我的简单解决方案:以编程方式将进度条的 Y 设置为超级视图/布局的 Y

          protected void onLayout(boolean changed, int l, int t, int r, int b) {
              super.onLayout(changed, l, t, r, b);
          
              progressBar.setY(layout.getY() - progressBar.getHeight() / 2);
          }
          

          像魅力一样工作,适用于所有屏幕尺寸。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-12-23
            • 2019-05-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-02-14
            • 1970-01-01
            相关资源
            最近更新 更多