【问题标题】:Horizontal progress bar is not visible if placed above toolbar (For Android 5)如果放置在工具栏上方,水平进度条不可见(适用于 Android 5)
【发布时间】:2015-09-08 18:25:05
【问题描述】:

我尝试通过使用以下 XML 将水平进度条放置在工具栏的顶部。

my_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- Toolbar -->
        <include layout="@layout/toolbar"/>

        <!-- http://stackoverflow.com/questions/14171471/remove-vertical-padding-from-horizontal-progressbar -->
        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="8dp"
            android:id="@+id/progress_bar"
            android:layout_gravity="top"

            android:layout_marginBottom="0dp"
            android:layout_marginTop="-3dp"

            android:progress="2000"
            android:max="10000" />

    </FrameLayout>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="8dp"
            android:id="@+id/progress_bar2"
            android:layout_gravity="top"

            android:layout_marginBottom="0dp"
            android:layout_marginTop="-3dp"

            android:progress="2000"
            android:max="10000" />

    </FrameLayout>
</LinearLayout>

工具栏.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >

    <!-- android:elevation="4dp" is used due to http://www.google.com/design/spec/what-is-material/elevation-shadows.html#elevation-shadows-elevation-android- -->

</android.support.v7.widget.Toolbar>

这在 Android 4+ 下工作得非常好。这是Android 4+的屏幕截图

但是,对于 Android 5+,水平进度条在工具栏的顶部是不可见的。

如果我删除线

<include layout="@layout/toolbar"/>

我将在 Android 5+ 中获得以下屏幕截图

好像顶部进度条被工具栏挡住了?但是,我认为在FrameLayout 中,进度条的 z 顺序高于工具栏?

请问,在 Android 5+ 中,如果放置在工具栏上方,如何使水平进度条可见

【问题讨论】:

    标签: android


    【解决方案1】:

    在 Android 5.0+ 设备上,在确定组件的 z 顺序时会考虑高程 - 高程的组件明显高于低高程的组件,即使在 XML 文件中较早声明了高程项(通常会导致它落后)。

    您可以向您的ProgressBar 添加一个海拔,与Toolbar 的海拔相匹配 - 这将确保与以前版本的 Android 中相同的 z 排序工作。

    【讨论】:

    • 这是一个很棒的提示!为了提高进度条的 z 顺序,我花了相当长的时间来试验各种技术,例如 bringToFront。他们失败了。你的建议很好用。谢谢!
    【解决方案2】:

    尝试使用这个:

    <RelativeLayout
            android:id="@+id/container_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar" />
    
           // try to set your progress bar here
           // on top of the toolbar
    
        </RelativeLayout>
    

    【讨论】:

    • 但是,我不希望进度条占用工具栏的空间。即,顶部进度条不应“下推”工具栏。
    • 但是,LinearLayout 会将工具栏向下推。我不想向下推工具栏。我希望进度条与工具栏重叠,进度条的 z 顺序更高。
    • 这就是我使用RelativeLayout 发布的原因。如果您将进度条放在工具栏的顶部,您只需要 Visibility.GONE 和 Visibility.VISIBLE 位于工具栏的顶部。 RelativeLayout 不会下推工具栏
    • 应该使用什么RelativeLayout.LayoutParams?因为我从未遇到过允许堆栈重叠的 RelativeLayout。
    • 此任务不需要 LayoutParams。将进度条放在我的代码示例的注释部分。它会按你的意愿工作。为了更好看,放置具有匹配宽度和定义高度的进度条(以确保它不会以任何方式与工具栏混淆)
    猜你喜欢
    • 2014-07-24
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 2014-04-06
    • 2021-11-05
    相关资源
    最近更新 更多