ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 如何在Modal BottomSheet对话框中添加底部布局?

2019-07-06 03:25:11  阅读:170  来源: 互联网

标签:java android dialog


我想使用BottomSheetDialogFragment实现这样的设计.但问题是我拖动时底部布局会滚动.我希望底部布局始终在底部,直到BottomSheetDialogFragment被解除.

请找截图

ButtonSheetDialogFragment

这是我的代码

bottomsheet_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--The main content goes over here-->
    </LinearLayout>

    <!--The footer view-->
    <LinearLayout
        android:id="@+id/footer_purchase_layout"
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        android:weightSum="1">

        <TextView
            android:id="@+id/txt_cancel_purchase"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:gravity="left|center_vertical"
            android:text="CLOSE"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/txt_item_purchase_action"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:gravity="right|center_vertical"
            android:text="ADD STICKERS"
            android:textSize="14sp" />

    </LinearLayout>
</FrameLayout>

ModalBottomSheetFragment.java

public class ModalBottomSheetFragment extends BottomSheetDialogFragment {

    public ModalBottomSheetFragment() {}

    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {

        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                dismiss();
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {}
    };

    @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
    @Override
    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);

        View mContentView = View.inflate(getContext(), R.layout.bottomsheet_layout, null);
        dialog.setContentView(mContentView);
        CoordinatorLayout.LayoutParams layoutParams =
                (CoordinatorLayout.LayoutParams) ((View) mContentView.getParent()).getLayoutParams();
        CoordinatorLayout.Behavior mBehavior = layoutParams.getBehavior();
        if (mBehavior != null && mBehavior instanceof BottomSheetBehavior) {
            ((BottomSheetBehavior) mBehavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
            int height = getScreenHeight(getActivity());
            final double desiredHeight = 0.85 * height;
            mContentView.getLayoutParams().height = height;
            ((BottomSheetBehavior) mBehavior).setPeekHeight((int) desiredHeight);

        }
    }

    public static int getScreenHeight(Context ctx) {
        DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
        return metrics.heightPixels;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Button mShowBottomSheetDialog;
    private BottomSheetDialogFragment bottomSheetDialogFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mShowBottomSheetDialog = (Button) findViewById(R.id.showBottomSheet);
        mShowBottomSheetDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomSheetDialogFragment = new ModalBottomSheetFragment();
                bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
            }
        });
    }
}

解决方法:

使用此布局而不是您的布局….

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:orientation="vertical">
        <!--The main content goes over here-->
    </LinearLayout>

     </ScrollView>

    <!--The footer view-->
    <LinearLayout
        android:id="@+id/footer_purchase_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="1">

        <TextView
            android:id="@+id/txt_cancel_purchase"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:gravity="left|center_vertical"
            android:text="CLOSE"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/txt_item_purchase_action"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:gravity="right|center_vertical"
            android:text="ADD STICKERS"
            android:textSize="14sp" />

    </LinearLayout>
    </LinearLayout>
</FrameLayout>

注意: – 将您的布局重量划分为不同的视图,这在您的布局中是不变的……

标签:java,android,dialog
来源: https://codeday.me/bug/20190706/1393662.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有