【问题标题】:Align ProgressBar horizontal to top (remove gap) [duplicate]将 ProgressBar 水平对齐到顶部(消除间隙)[重复]
【发布时间】:2013-08-30 06:48:06
【问题描述】:

我正在尝试在框架顶部放置一个水平进度条。但似乎有我根本无法删除的填充。有什么帮助吗?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
<ProgressBar android:id="@+id/flv_progress_bar"
             android:layout_width="fill_parent"
             style="@android:style/Widget.Holo.ProgressBar.Horizontal"
             android:indeterminateOnly="true"
             android:layout_height="wrap_content"
             android:layout_gravity="top"/>
</FrameLayout>

【问题讨论】:

    标签: android android-layout actionbarsherlock


    【解决方案1】:

    Pork'n'Bunny 的回答适用于 API 级别 16 及更高级别的设备。该解决方案适用于所有设备。 只需在 XML 中向上推 6dp。

     <ProgressBar
            android:id="@+id/progressbar_Horizontal"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:layout_alignParentTop="true"
            android:layout_marginTop="-6dp" />
    

    【讨论】:

    • 这对默认样式的更改有多强大?它们是否可以保证永远保持相同的外观和工作方式,或者理论上可以随时用将其推高 6 像素会产生问题的东西(例如不可见的进度条)替换它们?
    • 这不适用于所有设备。在 Galaxy S6 上,即使有 -6dp 上边距,进度条上方的空白区域仍然可见。
    • 更好的解决方案是
    【解决方案2】:

    从外观上看,我认为它是由默认样式中使用的图像引起的

    style="@android:style/Widget.Holo.ProgressBar.Horizontal"
    

    (我指的是这张图片:@android:drawable/progress_indeterminate_horizontal_holo@android:drawable/progress_horizontal_holo_dark

    这些图片有很多画布背景;因此它显示了这个差距。

    我的建议:

    使用自定义图像来设置 PregressBar 的样式。一种没有太多画布间距的。

    【讨论】:

    • 这最终是正确的答案。我将不得不编辑所有 holo drawable 的版本(9 个补丁)以消除填充。
    【解决方案3】:

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

    1. 复制了所有 8 个不确定的水平进度条可绘制对象
    2. 使用一些图像操纵器编辑了可绘制对象并删除了不必要的填充
    3. 从 android 平台复制名为 progress_indeterminate_horizo​​ntal_holo.xml 的可绘制 XML
    4. 复制样式 Widget.ProgressBar.Horizo​​ntal 及其父项
    5. 在布局中手动设置样式和 min_height

    这里是progress_indeterminate_horizo​​ntal_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" />
    

    样式资源已复制到我的本地样式文件中。

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

    最后,在我的本地布局文件中将最小高度设置为 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" />
    

    【讨论】:

    • 谢谢苏斌!我实际上还没有开始这样做,因为我不确定除了编辑一个可绘制对象之外还需要做多少,现在我们都知道了。感谢分享。
    • 问题是关于?android:attr/progressBarStyleHorizontal 而不是@style/Widget.ProgressBar.Horizontal
    【解决方案4】:

    想出了另一个解决方案,因为可绘制对象按屏幕截图中的预期工作。只需向上推 6dp。

        mProgress = (ProgressBar) view.findViewById(R.id.fwv_progress);
        mProgress.animate().withLayer().yBy(-6.0f*getResources().getDisplayMetrics().density).setDuration(0).start();
    

    【讨论】:

    • 它只适用于 API 级别 16 及以上。有什么方法可以解决 API 8 及以上的问题吗?
    【解决方案5】:

    我认为问题出在高度属性上。尝试将布局高度设置为某个值

    <ProgressBar
                android:id="@+id/progressBar"
                android:max="100"
                android:layout_width="match_parent"
                android:layout_height="2dp"
                style="@android:style/Widget.ProgressBar.Horizontal"
                /> 
    

    【讨论】:

    • 如果是这种情况,那么您应该在样式文件中将可绘制对象更改为自定义的可绘制对象
    猜你喜欢
    • 2012-09-06
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 2021-06-18
    相关资源
    最近更新 更多