当前位置:  开发笔记 > 编程语言 > 正文

底板景观问题

如何解决《底板景观问题》经验,为你挑选了3个好方法。

在横向模式下显示底部对话框时,我的行为有误.问题发生在24. +版本的设计库中.根据下图,底片仅在横向上无法正确显示.我正在使用BottomSheetDialog类,我正在按照本教程:http://www.skholingua.com/blog/bottom-sheet-android,在我发布的应用程序中也会出现问题.

我测试了25. +版本,问题没有解决.

错误在横向24,25 +库中

在景观中出错

23. + Library中的相同示例

23. + Library中的相同示例

主要活动

public class MainActivity extends AppCompatActivity {
CoordinatorLayout coordinatorLayout;
private BottomSheetBehavior 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中





    

bottom_sheet_layout.xml









hyena.. 9

这是一个解决方法.(注意:此代码在活动中)

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


urSus.. 5

Google员工已按预期完成了此操作,这是一种解决方法

屏幕高度比例有一个不可思议的常数,似乎是min(actualwidth,themewidth),这显然不适用于手机环境,因此将其覆盖更大的值






Carlos Danie.. 5

My personal choice to solve this problem is, after onCreateView, you already have the main view of the BottomSheetDialogFragment

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

And the onStart will be executed after you have the View created, so you can play with the behaviour:

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

Then you will always have the Bottom Sheet expanded



1> hyena..:

这是一个解决方法.(注意:此代码在活动中)

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



2> urSus..:

Google员工已按预期完成了此操作,这是一种解决方法

屏幕高度比例有一个不可思议的常数,似乎是min(actualwidth,themewidth),这显然不适用于手机环境,因此将其覆盖更大的值







3> Carlos Danie..:

My personal choice to solve this problem is, after onCreateView, you already have the main view of the BottomSheetDialogFragment

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

And the onStart will be executed after you have the View created, so you can play with the behaviour:

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

Then you will always have the Bottom Sheet expanded

推荐阅读
云聪京初瑞子_617
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有