我发现如果父布局包含 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?
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/