我尝试通过以下 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 5.0+ 设备上,在确定组件的 z 顺序时会考虑标高 - 标高较高的组件明显高于标高较低的组件,即使标高较高的项目是在 XML 文件中较早声明的(通常会导致它在后面)。
您可以将海拔添加到您的ProgressBar
,匹配高程Toolbar
- 这将确保与以前版本的 Android 中相同的 z 顺序。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)