在 Fragment、Activity 和 DrawerLayout 组合上适合 SystemWindows

ade*_*190 4 android android-layout android-fragments navigation-drawer windowinsets

问题

该活动布局具有DrawerLayoutFrameLayout作为内容FragmentLinearLayout被打开和关闭的抽屉。用于内容的片段有一个FrameLayoutwithfitsSystemWindows标志和一个Toolbar.

如果我onCreate()按预期插入片段,则从FrameLayout位置 0Toolbar开始,从状态栏下方开始。但是在onCreate()相同的代码之外失败,fitsSystemWindows不能与FrameLayout.

重要的一点,我需要打开ToolbarfitsSystemWindows标记Fragment而不是打开Activity

如何在onCreate()此刻看到相同的行为?

代码

主要活动

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            window.setStatusBarColor(Color.TRANSPARENT);
        }

        setContentView(R.layout.activity);

        findViewById(R.id.item).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Out of onCreate fails
                replaceFragment();
            }
        });

        // With onCreate all OK
        replaceFragment();
    }

    private void replaceFragment() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content, new MainFragment())
                .commit();
    }

}
Run Code Online (Sandbox Code Playgroud)

主要布局

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <LinearLayout
        android:layout_width="256dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#ff0000"
        android:orientation="vertical"
        tools:layout_gravity="">

        <View
            android:layout_width="match_parent"
            android:layout_height="168dp"
            android:layout_marginBottom="16dp"
            android:background="#00ffff"/>

        <TextView
            android:id="@+id/item"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="#ff00ff"
            android:gravity="center"
            android:text="Click me"/>

    </LinearLayout>

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

分段

public class MainFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 
                                                @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment, container, false);
    }

}
Run Code Online (Sandbox Code Playgroud)

片段布局:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00ff00"
        android:fitsSystemWindows="true">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:background="#0000ff"/>

    </FrameLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

例子

LinearLayout被打开为红色。在FrameLayoutfitsSystemWindows标志是绿色,而工具栏显示为蓝色,青色视图工作的时候,它需要在开始y位置为0的预期是绿色和蓝色像第一次,但在第二次蓝色独特的看法。

在此处输入图片说明

azi*_*ian 5

所以,问题是,当内容被更改时,系统将不再将这些插入分派到视图层次结构。

这可以通过ViewCompat.requestApplyInsets(View)API轻松解决。这将迫使系统WindowInsets再次调度。

要求View.onApplyWindowInsets(WindowInsets)执行新的调度。这回落到View.requestFitSystemWindows()可用的地方。

因此,在您的示例中执行此更改将导致预期的行为:


    private void replaceFragment() {
      getSupportFragmentManager().beginTransaction()
          .replace(R.id.content, new MainFragment())
          // NOTE, we are performing `commitNow()` instead of ordinary `commit()`,
          // Because we want this commit to happen sychronously/immediately
          .commitNow();

      // Ask the framework to dispatch window insets once more to the root of your view hierarchy
      ViewCompat.requestApplyInsets(findViewById(R.id.drawer));
    }

在此处输入图片说明