Ich bekomme ein falsches Verhalten, wenn im Bottom-Sheet-Modus ein Bottom-Sheet-Dialog angezeigt wird. Das Problem tritt in der Version 24. + der Entwurfsbibliothek auf. Entsprechend der Abbildung unten wird das untere Blatt nur in der Landschaft nicht richtig angezeigt. Ich benutze die BottomSheetDialog-Klasse und folge diesem Tutorial: http://www.skholingua.com/blog/bottom-sheet-Android . In meinen veröffentlichten Apps tritt das Problem ebenfalls auf.
Ich habe die Version 25. + getestet und das Problem wurde nicht gelöst.
Fehler In Landschaft 24, 25. + Bibliothek
Dasselbe Beispiel in 23. + Bibliothek
Hauptaktivität
public class MainActivity extends AppCompatActivity {
CoordinatorLayout coordinatorLayout;
private BottomSheetBehavior<View> mBottomSheetBehavior;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.main_content);
textView = (TextView) findViewById(R.id.textView);
View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);
//For your bottom sheet to be displayable, you need to create a BottomSheetBehavior.
//This is created by getting a reference to the container view and calling BottomSheetBehavior.from() on that container.
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_DRAGGING:
break;
case BottomSheetBehavior.STATE_COLLAPSED:
mBottomSheetBehavior.setPeekHeight(0);
break;
case BottomSheetBehavior.STATE_EXPANDED:
break;
case BottomSheetBehavior.STATE_HIDDEN:
break;
case BottomSheetBehavior.STATE_SETTLING:
break;
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
/**
* For persistent bottom sheet to work, your layout should contain a coordinator layout,
* and then in any child view of your coordinator layout, you can make it as a persistent bottom sheet
* by adding a custom property app:layout_behavior and use behavior_peekHeight to define how much
* of the Bottom Sheet you want visible.
*/
textView.setText(R.string.dynamic_persistent_txt);
mBottomSheetBehavior.setPeekHeight(300);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
break;
case R.id.button2:
/**
* You can also display a Dialog in place of a View in the bottom sheet.
* To do this, get the view from getLayoutInflater and pass it setContentView of the Dialog.
*/
View view = getLayoutInflater().inflate(R.layout.bottom_sheet_layout, null);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(R.string.dialog_modal_txt);
BottomSheetDialog dialog = new BottomSheetDialog(this);
dialog.setContentView(view);
dialog.show();
break;
case R.id.button3:
/**
* You can also display a Fragment in place of a View in the bottom sheet.
* To do this, you class that extends BottomSheetDialogFragment.
*/
BottomSheetDialogFragment bottomSheetDialogFragment = new BottomSheetDialogFragmentExample();
bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
break;
}
}
activity_main.xml
<Android.support.design.widget.CoordinatorLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/main_content"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:fitsSystemWindows="true">
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical"
Android:paddingTop="24dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<Button
Android:id="@+id/button1"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:onClick="onClick"
Android:text="Dynamic BottomSheet" />
<Button
Android:id="@+id/button2"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:onClick="onClick"
Android:text="BottomSheetDialog" />
<Button
Android:id="@+id/button3"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:onClick="onClick"
Android:text="BottomSheetDialogFragment" />
</LinearLayout>
<LinearLayout
Android:id="@+id/bottom_sheet"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:elevation="4dp"
Android:minHeight="120dp"
Android:orientation="vertical"
Android:padding="@dimen/activity_vertical_margin"
app:behavior_peekHeight="120dp"
app:layout_behavior="Android.support.design.widget.BottomSheetBehavior">
<include layout="@layout/bottom_sheet_layout" />
</LinearLayout>
</Android.support.design.widget.CoordinatorLayout>
bottom_sheet_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:background="@color/CreamyGreen"
Android:orientation="vertical">
<TextView
Android:id="@+id/textView"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="@string/static_persistent_txt"
Android:padding="16dp"
Android:textAppearance="?android:attr/textAppearanceMedium"
Android:textColor="@Android:color/white" />
<TextView
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:padding="16dp"
Android:text="@string/ipsum"
Android:textColor="@Android:color/white"
Android:textSize="16sp" />
</LinearLayout>
dies ist eine Umgehung. (Hinweis: Dieser Code befindet sich in einer Aktivität)
View sheetView;//class level variable
private void setUpBottomSheetDialog() {
bottomSheetDialog = new BottomSheetDialog(this);
LayoutInflater inflater = LayoutInflater.from(this);
sheetView = inflater.inflate(R.layout.bottom_sheet_image_source, (ViewGroup) this.getWindow().getDecorView().getRootView(), false);
bottomSheetDialog.setContentView(sheetView);
BottomSheetBehavior mBehavior = BottomSheetBehavior.from((View) sheetView.getParent());
bottomSheetDialog.setOnShowListener(dialogInterface -> {
mBehavior.setPeekHeight(sheetView.getHeight());//get the height dynamically
});
}
Google-Jungs haben dies als bestimmungsgemäß geschlossen, hier ist ein Workaround
Es gibt eine magische Konstante des Bildschirmhöhenverhältnisses, die min (tatsächliche Breite, Breite) zu tun scheint, was offensichtlich nicht gut funktioniert Bei Telefonlandschaften. Überschreiben Sie es daher mit etwas Größerem
<style name="Theme.Main.Reader">
...
<item name="bottomSheetDialogTheme">@style/ReaderBottomSheelDialog</item>
</style>
<style name="ReaderBottomSheelDialog" parent="Theme.Design.BottomSheetDialog">
<item name="bottomSheetStyle">@style/BottomSheetStyle</item>
</style>
<style name="BottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="behavior_peekHeight">512dp</item>
</style>
Um ein BottomSheetDialogFragment immer vollständig geöffnet zu haben (auch im Querformatmodus), mache ich Folgendes.
In onCreateDialog erstellen Sie den BottomSheetDialog und Ihre Ansicht. Nachdem Sie diese Ansicht zum BottomSheetDialog hinzugefügt haben, können Sie BottomSheetBehavior abrufen, indem Sie das übergeordnete Element Ihrer Ansicht in BottomSheetBehavior.from () verwenden.
Dann rufen Sie beim Start des BottomSheetDialogFragment das BottomSheetBehavior.setState mit STATE_EXPANDED auf.
BottomSheetBehavior mBottomBehavior;
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog dialog = new BottomSheetDialog(getContext());
mBinding = SomeBinding.inflate(LayoutInflater.from(getContext()));
dialog.setContentView(mBinding.getRoot());
mBottomBehavior = BottomSheetBehavior.from((View) mBinding.getRoot().getParent());
return dialog;
}
public void onStart() {
super.onStart();
mBottomBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
Ich hoffe das hilft jemandem.
Meine persönliche Wahl zur Lösung dieses Problems ist, dass Sie nach onCreateView bereits die Hauptansicht des BottomSheetDialogFragment haben
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mainLayout = inflater.inflate(R.layout.fragment_auction_bottom_sheet_dialog, container, false);
return mainLayout;
}
Und das onStart wird ausgeführt, nachdem Sie die Ansicht erstellt haben, sodass Sie play mit dem Verhalten:
@Override
public void onStart() {
super.onStart();
//this expands the bottom sheet even after a config change
bottomSheetBehavior = BottomSheetBehavior.from((View) mainLayout.getParent());
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
Dann haben Sie immer das untere Blatt erweitert
BottomSheetDialog-Dialog; BottomSheetBehavior bottomSheetBehavior;
private void createBottomSheet() {
if (dialog == null){
dialog = new BottomSheetDialog(this);
final View view = LayoutInflater.from(this).inflate(R.layout.setting_dialog,
(ViewGroup)this.getWindow().getDecorView().getRootView(),false);
dialog.setContentView(view);
bottomSheetBehavior = BottomSheetBehavior.from((View)view.getParent());
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
bottomSheetBehavior.setPeekHeight(view.getHeight());
}
});
}
}