Android:fitsSystemWindows和newline干扰bottomSheets

TWL*_*TWL 5 android android-layout android-view android-statusbar

我发现,如果父布局包含android:fitsSystemWindows="true",当发生与视图相关的操作时,它将干扰我的BottomSheets定位.

特别是我遇到的那个:文本视图中的换行符将触发底部表格偏移系统/通知栏的高度.

newline + fitsSystemWindows =推倒我的底部表格

我删除了所有不相关的东西到按钮,textview和底部表格.

  • Button2:setText("1\n2")是魔术发生的地方

一旦我删除 android:fitsSystemWindows="true",它一切正常,没有更奇怪的行为,但我失去了systemSystemWindows颜色在系统/通知栏中的效果.

我也尝试给自己的bottomSheet布局android:fitsSystemWindows="false",但它没有任何效果.

如何在我的bottomSheets上没有这种奇怪的偏移行为的情况下如何实现fitsSystemWindows = true?

你自己看! 在此输入图像描述

public class Test extends AppCompatActivity {
    private BottomSheetBehavior bottomSheetBehavior;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_test);

        LinearLayout bottomSheet = (LinearLayout) findViewById(R.id.root_btmsheet);
        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
        bottomSheet.post(new Runnable() {
            @Override
            public void run() {
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            }
        });

        ((Button) findViewById(R.id.btn1)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((TextView) findViewById(R.id.info1)).setText("1");
            }
        });
        ((Button) findViewById(R.id.btn2)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // the culprit, Mr. Newline
                ((TextView) findViewById(R.id.info1)).setText("1\n2");
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

act_test.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"   <-- the other culprit, Ms. Fits
    tools:context=".Test">

    <!--<include layout="@layout/act_test_content" />-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:orientation="vertical"
        android:id="@+id/root_content">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1" />
            <Button
                android:id="@+id/btn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2" />
        </LinearLayout>

        <TextView
            android:id="@+id/info1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#e0e0e0" />

    </LinearLayout>

    <!--<include layout="@layout/act_test_btmsheet" />-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:layout_behavior="@string/bottom_sheet_behavior"
        android:orientation="vertical"
        app:behavior_hideable="false"
        app:behavior_peekHeight="50dp"
        android:background="#5533b5e5"
        android:id="@+id/root_btmsheet">

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

小智 0

即使使用支持库 25.1.0,我也遇到了类似的问题。但我后来发现这可能是因为没有使用 CollapsingToolbarLayout。我把它包括进去了!...即使由于换行而发生任何布局更改,它也表现正常