亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 BottomSheetDialogFragment 設置最大高度?

如何在 BottomSheetDialogFragment 設置最大高度?

精慕HU 2024-01-17 21:04:36
我有一個 BottomSheetDialogFragment,我需要在此對話框中設置我的高度。我需要,當用戶點擊按鈕時,將出現對話框并填充屏幕的 85%。這個怎么做?
查看完整描述

6 回答

?
茅侃侃

TA貢獻1842條經驗 獲得超21個贊

BottomSheetDialogFragment:


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

    super.onViewCreated(view, savedInstanceState)


    val offsetFromTop = 200

    (dialog as? BottomSheetDialog)?.behavior?.apply {

        isFitToContents = false

        setExpandedOffset(offsetFromTop)

        state = BottomSheetBehavior.STATE_EXPANDED

    }

}


查看完整回答
反對 回復 2024-01-17
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

你可以這樣做:


public class MyBottomSheetDialog extends BottomSheetDialogFragment {


   //....


   @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {

    Dialog dialog = super.onCreateDialog(savedInstanceState);

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {

      @Override public void onShow(DialogInterface dialogInterface) {

        BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialogInterface;

        setupRatio(bottomSheetDialog);

      }

    });

    return  dialog;

  }


  private void setupRatio(BottomSheetDialog bottomSheetDialog) {

    //id = com.google.android.material.R.id.design_bottom_sheet for Material Components

    //id = android.support.design.R.id.design_bottom_sheet for support librares

    FrameLayout bottomSheet = (FrameLayout) 

        bottomSheetDialog.findViewById(R.id.design_bottom_sheet);

    BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);

    ViewGroup.LayoutParams layoutParams = bottomSheet.getLayoutParams();

    layoutParams.height = getBottomSheetDialogDefaultHeight();

    bottomSheet.setLayoutParams(layoutParams);

    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);

  }

  private int getBottomSheetDialogDefaultHeight() {

    return getWindowHeight() * 85 / 100;

  }

  private int getWindowHeight() {

    // Calculate window height for fullscreen use

    DisplayMetrics displayMetrics = new DisplayMetrics();

    ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    return displayMetrics.heightPixels;

  }


}

https://img1.sycdn.imooc.com/65a7d0a400011a1b02830505.jpg

查看完整回答
反對 回復 2024-01-17
?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

始終展開到完整高度可以在BottomSheetDialogFragment()的onCreateDialog中設置


    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

    val dialog = BottomSheetDialog(requireContext(), theme)

    dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED

    dialog.behavior.skipCollapsed = true

    return dialog

}

最大高度可以在onCreateView中設置


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,

        savedInstanceState: Bundle?

    ): View? {

        val view = inflater.inflate(R.layout.bottom_sheet_dialog, container, false)

        // Set max height to half screen

        view.findViewById<ConstraintLayout>

(R.id.root_layout_of_bottom_sheet).maxHeight =

            (resources.displayMetrics.heightPixels * 0.5).toInt()

        return view

    }


查看完整回答
反對 回復 2024-01-17
?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

在 Kotlin 和 Jetpack 中編寫:


val configuration = LocalConfiguration.current

val screenWidth = configuration.screenWidthDp.divideToPercent(0.40f) // total screen width size of 40 percentage

val screenHeight = configuration.screenHeightDp.divideToPercent(0.95f) // total screen height size of 95 percentage

configuration.screenWidth --> 給出屏幕尺寸


divideToPercent(0.40f) --> 它將給出屏幕上的一些百分比


查看完整回答
反對 回復 2024-01-17
?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

非常容易處理


開始吧


override fun onStart() {

    super.onStart()

    val height = Resources.getSystem().displayMetrics.heightPixels


    // in this case I want set max height half of the device height

    val maxHeight = (height * 0.5).toInt()


    val behaviour = BottomSheetBehavior.from(requireView().parent as View)



    /* Note that If you want to display max height 

      as per your bottomsheet view 

      val maxHeight = WindowManager.LayoutParams.MATCH_PARENT */


    // now set your max peek height 

    behavior.peekHeight = maxHeight 

}

從上面的情況來看,如果你設置maxHeight = WindowManager.LayoutParams.MATCH_PARENT


當您在bottmsheet上顯示一些固定的內容時,這很簡單onCreateView但是有時如果更新UI后Main Thread 它可能不會顯示實際高度有時那么最好使用viewTreeObserver


override fun onStart() {

    super.onStart()

    

    view?.viewTreeObserver?.addOnGlobalLayoutListener {

        val behavior = BottomSheetBehavior.from(view!!.parent as View)

        behavior.peekHeight = WindowManager.LayoutParams.MATCH_PARENT

    }



}



查看完整回答
反對 回復 2024-01-17
?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

嘗試這個 :


 DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();

     int height = displayMetrics.heightPixels;

     int maxHeight = (int) (height*0.80);



 View bottomSheet = findViewById(R.id.bottom_sheet);  

 BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);         

 behavior.setPeekHeight(maxHeight);

并在您的 XML 中:


<LinearLayout 

    android:id="@+id/bottom_sheet"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    app:behavior_hideable="true"

    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"




      .

      .

      .


</LinearLayout>


查看完整回答
反對 回復 2024-01-17
  • 6 回答
  • 0 關注
  • 524 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號