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

本文介绍了如何在Java的BottomSheetDialogFragment中创建一个始终保持在底部的布局,即使在拖动时也不会滚动。解决方案是通过修改布局文件,使用权重分配给不同的视图来实现。
摘要由CSDN通过智能技术生成

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

请找截图

5c51b925df47f27ebeb1ae81df8f7339.png

这是我的代码

bottomsheet_layout.xml

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent">

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">

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" />

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" />

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

}

});

}

}

解决方法:

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

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="10"

android:orientation="vertical">

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">

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" />

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" />

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

标签:java,android,dialog

来源: https://codeday.me/bug/20190706/1393662.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值