- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法让我的 ViewPager 页面尊重状态栏高度。
我的 ViewPager 有几个页面,其中一个页面上有一个图像需要放在半透明状态栏下。其余的页面只需要一个彩色状态栏,所以我的想法是在 fragment 布局上使用 fitSystemWindows="true"作为 viewpager 的页面。但这不起作用。似乎在第一次绘制时,只有第二页应用了填充。但是当滚动页面时。它消失了。所以在我看来,页面没有从 ViewPager 获得调度来应用插图。但我不知道如何实现它。
我试图做一些事情public static void setOnApplyWindowInsetsListener(View v, OnApplyWindowInsetsListener listener)
,但我无法开始工作。我的想法是以某种方式在每个新页面上应用插图。也许这是正确的方法,但我找不到调用此方法的正确位置。
那么有没有人可以帮助我解决这个问题?
我将在下面放置一些代码,以提供有关该问题的一些上下文。
Activity .xml
<..WindowInsetsFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
public class WindowInsetsFrameLayout extends FrameLayout {
public WindowInsetsFrameLayout(@NonNull Context context) {
this(context, null);
}
public WindowInsetsFrameLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public WindowInsetsFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// Look for replaced fragments and apply the insets again.
setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
@Override
public void onChildViewAdded(View parent, View child) {
requestApplyInsets();
}
@Override
public void onChildViewRemoved(View parent, View child) {
}
});
}
}
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.ViewPagerController
android:id="@+id/view_pager_controller"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:showConfirmButton="true" />
</android.support.constraint.ConstraintLayout>
<merge>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/tab_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread" />
...some other buttons for next en previous of the ViewPager...
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="wrap_content"
android:layout_height="@dimen/size_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_pager"
app:tabBackground="@drawable/tab_selector"
app:tabIndicatorHeight="0dp" />
</merge>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="handler"
type="fragmentX" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_white">
<android.support.constraint.ConstraintLayout
android:id="@+id/top_panel"
android:layout_width="0dp"
android:layout_height="@dimen/header_input_height"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true"
android:paddingEnd="@dimen/size_16"
android:paddingStart="@dimen/size_16"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageButton
android:id="@+id/close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/actionBarItemBackground"
android:minHeight="@dimen/size_48"
android:minWidth="@dimen/size_48"
android:onClick="@{(V) -> handler.onClose()}"
android:src="@drawable/ic_close"
app:layout_constraintBottom_toBottomOf="@id/icon_img"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/icon_img" />
<ImageView
android:id="@+id/icon_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/size_16"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_logo" />
<TextView
android:id="@+id/title_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/size_32"
android:fontFamily="@font/alvania_regular"
android:text="title or page"
android:textColor="@color/white"
android:textSize="@dimen/font_size_biggest"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/icon_img" />
<TextView
android:id="@+id/message_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/size_8"
android:alpha="0.87"
android:fontFamily="@font/frutiger_light"
android:gravity="center"
android:lineSpacingExtra="8sp"
android:text="sub title fo a page"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title_txt" />
</android.support.constraint.ConstraintLayout>
...more views
</android.support.constraint.ConstraintLayout>
</layout>
最佳答案
首先,您必须将全屏应用到您的 Activity ,以便 Activity 中的所有 fragment 都将在 fitSystemWindow(全屏)上,然后在每个 fragment 上您可以设置状态栏颜色,这将触发全屏或正常情况。如果 statubar 透明,则全屏显示 fragment 。
启用全屏:
window.decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
通过着色状态栏启用/禁用全屏:
activity?.window?.statusBarColor = Color.TRANSPARENT //Fullscreen
activity?.window?.statusBarColor = Color.TRANSPARENT //non-Fullscreen
只需在您的全屏可见或隐藏时启用禁用它
关于android - 如何在 ViewPager 的 fragment 页面上应用 fitSystemWindows?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51590884/
我有一个 View 需要锚定到屏幕底部。在全屏模式下,它需要位于最底部,但在临时非全屏模式(用户点击)期间,它需要向上移动以免被系统栏隐藏。文档说设置 android:fitsSystemWindow
我的应用程序有一个 Activity ,为每个部分托管不同的 fragment 。我最近通过将 fitSystemWindows 设置为 true 使状态栏变得半透明,这已将其设置为应用程序的背景色。
我很难理解 fitsSystemWindows 的概念,因为它会根据 View 做不同的事情。根据官方文档,这是一个 Boolean internal attribute to adjust view
我需要图像显示在状态栏下方(状态栏必须是透明的,不知道怎么做)但我无法让它工作,我已经尝试了很多布局示例。 这是我的 xml 布局:
我正在使用一个在主要 Activity 中有 3 个选项卡的应用程序。第二个选项卡的 fragment 有一个带有标题的 NestedScrollView,应该适合系统窗口。选项卡 fragment
我有一个使用 fitSystemWindows 能够在导航栏和状态栏后面绘制背景的应用程序 - 不幸的是,SnackBar 似乎忽略了容器中的 fitSystemWindows=true。我将问题归结
我有一个绑定(bind)到 ViewPager 的 MotionLayout 过渡。 private val pageChangedListener = object : SimpleOnPageCh
我有一个透明的状态/导航栏,当我使用默认布局(顶部/左侧)放置一个撰写元素时,它被放置在状态栏下方。在 xml 中我使用 fitsSystemWindows要解决这个问题,我怎样才能在 jetpack
我无法让我的 ViewPager 页面尊重状态栏高度。 我的 ViewPager 有几个页面,其中一个页面上有一个图像需要放在半透明状态栏下。其余的页面只需要一个彩色状态栏,所以我的想法是在 frag
我正在尝试在状态栏后面绘制 View ,如下所示: 我尝试使用推荐的技术来产生这种效果,但我明白了: 从屏幕截图中可以清楚地看出,我的应用内容都没有被绘制在状态栏后面。 有趣的是,Nav Drawer
当使用来自新的 Android 4.4 KitKat API 的半透明状态栏和导航栏时,将 fitsSystemWindows="true" 和 clipToPadding="false" 设置为 L
我是一名优秀的程序员,十分优秀!