Android:fitsSystemWindows 和换行干扰 bottomSheets

标签 android android-layout android-view android-statusbar

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

特别是我遇到的那个: TextView 中的换行符将触发底页偏移系统/通知栏的高度。

newline + fitsSystemWindows = 将我的 bottomsheet 向下推

我删除了所有不相关的东西,包括按钮、textview 和 bottomsheet。

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

一旦我删除 android:fitsSystemWindows="true",一切正常,不再有奇怪的行为,但我失去了 fitsSystemWindows 颜色的效果系统/通知栏。

我还尝试为我的 bottomSheet 布局提供自己的 android:fitsSystemWindows="false",但没有任何效果。

如果我的 bottomSheets 没有这种奇怪的偏移行为,我如何才能同时实现 fitsSystemWindows=true?

自己看吧! enter image description here

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");
            }
        });
    }
}

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>

最佳答案

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

关于Android:fitsSystemWindows 和换行干扰 bottomSheets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38781169/

相关文章:

javascript - 如何在 Android 浏览器上使用 javascript 打开/处理多个链接

java - 从 ColorPickerDialog.java 中检索颜色信息

android - 开关按钮在 Android 中不显示 ON 和 OFF 文本

android - 如何将 Android SnackBar 调整到屏幕上的特定位置

Android:形状绘图中的填充有什么用?

android - 动态删除 View 或覆盖 View Android

Android:调试 native 代码时出现 Eclipse gdb 错误

java - 安卓锁屏

Android 应用屏幕尺寸不适合设备

android - 创建镜像布局