问题
I have found out that if the parent layout contains android:fitsSystemWindows="true"
, it will interfere with my BottomSheets positioning, when a view-related action happens.
Specifically the one I'm encountering: where a newline in a textview will trigger the bottomsheets to offset by the height of the system/notif-bar.
newline + fitsSystemWindows = shoves my bottomsheet down
I eliminated all irrelevant stuff down to buttons, textview and the bottomsheet.
- Button2:
setText("1\n2")
is where the magic happens
As soon as I remove android:fitsSystemWindows="true"
, it's all ok, no more strange behavior, but I lose the effect of how fitsSystemWindows colors in the system/notif-bar.
I also tried to give my bottomSheet layout its own android:fitsSystemWindows="false"
, but it has had no effect.
How can I achieve both fitsSystemWindows=true without this strange offset behavior on my bottomSheets?
See for yourself!
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>
回答1:
I too had a similar problem even with support library 25.1.0. But I later figured out that it could have been because of not using a CollapsingToolbarLayout. I included it and bang!.. it is behaving normally even after any layout changes due to linebreaks
来源:https://stackoverflow.com/questions/38781169/android-fitssystemwindows-and-newline-interfering-with-bottomsheets