web-dev-qa-db-fra.com

Problème de paysage de fond

J'ai un mauvais comportement lorsque j'affiche un dialogue de fond en mode paysage. Le problème se produit dans la version 24. + de la bibliothèque de conception. Selon l'image ci-dessous, la feuille inférieure ne s'affiche pas correctement uniquement en mode paysage. J'utilise la classe BottomSheetDialog et je suis ce tutoriel: http://www.skholingua.com/blog/bottom-sheet-Android , dans mes applications publiées, le problème se produit également.

J'ai testé la version 25. + et le problème n'a pas été résolu.

Erreur dans le paysage 24, 25. + Bibliothèque

 Error In landscape

Même exemple en 23. + Bibliothèque

 same example in 23.+ Library

Activité principale

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>
17
Fernando Andrauss

c'est un travail autour. (Remarque: ce code est dans une activité)

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
    });
}
6
hyena

Les gars de Google ont fermé ce jeu car ils fonctionnaient comme prévu. Voici une solution de contournement.

Il existe une constante magique du rapport de hauteur d’écran, ce qui semble faire min (largeur réelle, largeur), qui ne fonctionne évidemment pas bien.

<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>
3
urSus

Pour avoir un BottomSheetDialogFragment toujours ouvert complètement développé (également en mode paysage), je fais ce qui suit.

Dans onCreateDialog, vous créez le BottomSheetDialog et votre vue. Après avoir ajouté cette vue au BottomSheetDialog, vous pouvez obtenir le BottomSheetBehavior en utilisant le parent de votre vue dans BottomSheetBehavior.from ().

Ensuite, dans onStart of the BottomSheetDialogFragment, vous appelez BottomSheetBehavior.setState avec STATE_EXPANDED.

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

J'espère que ça aidera quelqu'un.

2
Pepijn

Dialogue BottomSheetDialog; 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());
            }
        });
    }
}
0
Tarif Chakder