gpt4 book ai didi

android - 如何在 ViewPager 的 fragment 页面上应用 fitSystemWindows?

转载 作者:行者123 更新时间:2023-12-03 14:56:55 27 4
gpt4 key购买 nike

我无法让我的 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" />

这是一个自定义布局管理器,用于支持更改 fragment ( fragment 转换)并再次应用 windowInset。仅供引用,这是代码:
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) {

}
});
}
}

然后在某个时间点,我将以下 fragment 加载到 fragment_container。

fragmentWithViewpager.xml
<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>

ViewPagerController 是 ViewPager、TabLayout(用于 ViewPager 的页面指示)和一些我在下面的布局中省略的按钮的持有者。

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

最后,ViewPager 的大部分页面都有这样的布局:
<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
只需在您的全屏可见或隐藏时启用禁用它
阅读有关它的帖子:
https://medium.com/jama9779/single-activity-with-fullscreen-simple-fragments-fefe7f041765

关于android - 如何在 ViewPager 的 fragment 页面上应用 fitSystemWindows?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51590884/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com