【问题标题】:Android bottom sheet design安卓底片设计
【发布时间】:2020-05-17 17:17:38
【问题描述】:

有什么办法可以设计如下的底部片断:

我需要将底页的宽度设置为屏幕的 1/4。有可能实现吗?

感谢您的帮助。

【问题讨论】:

    标签: android kotlin bottom-sheet android-bottomsheetdialog


    【解决方案1】:

    您可以使用this 库获得相同的结果。或者只是制作相同的CornerSheetBehavior。如您所见,它只是一个带有 translationX 视图管理的 BottomSheetBehavior

    【讨论】:

    • 图书馆好伙伴!非常感谢
    【解决方案2】:

    像往常一样为你的底部工作表创建一个布局文件,并将宽度设置为你需要的:

        <?xml version="1.0" encoding="utf-8"?>
        <androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/sheet_background"
        android:layout_width="100dp"
        android:layout_height="match_parent">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="100dp"
            android:layout_height="match_parent"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
            app:behavior_peekHeight="150dp"
            android:id="@+id/drag_up_from_here">
    
            <!-- Bottom sheet contents here -->
    
            </androidx.constraintlayout.widget.ConstraintLayout>
    
        </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    然后,为了让它贴在右侧,您可以以编程方式将底部工作表附加到您的布局中。这就是我添加底部表格的方式:

        val inflater = getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val bottomSheet = inflater.inflate(R.layout.bottomsheet_layout, null)
        val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet.findViewById<View>(R.id.drag_up_from_here)
    
        // Allow user to drag part of bottom sheet
        bottomSheet.findViewById<LinearLayout>(R.id.drag_up_from_here).setOnClickListener {
            if (bottomSheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED)
            } else {
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED)
            }
        }
    
        // Create popup window
        val bottomSheetPopup = PopupWindow(popupViewLower,
                                        LinearLayout.LayoutParams.WRAP_CONTENT,
                                        LinearLayout.LayoutParams.MATCH_PARENT,
                                        false)
    
        // Get the view you want the bottom sheet added to
        val currentView = findViewById<ConstraintLayout>(R.id.main_view)
    
        // Display the popup window
        bottomSheetPopup.showAtLocation(currentView, Gravity.BOTTOM, currentView.measuredWidth, 0)
    

    让它在屏幕右侧的关键是这部分:

    bottomSheetPopup.showAtLocation(currentView, Gravity.BOTTOM, currentView.measuredWidth, 0)
    

    currentView.measueredWidth 作为 x 值传递,将底部工作表一直向右移动。

    编辑:我意识到您还要求宽度恰好是屏幕宽度的 1/4。为此,以编程方式将底部工作表布局的宽度设置为 currentView.measuredWidth / 4。这篇文章很好地解释了这一点:Android view layout_width - how to change programmatically?

    【讨论】:

    • 感谢您在代码和参考资料旁边提供解释。谢谢。
    猜你喜欢
    • 2012-07-02
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    相关资源
    最近更新 更多