简体   繁体   English

将 Material Persistent Bottom Sheet 高度设置为屏幕高度的一半?

[英]Set Material Persistent Bottom Sheet height to half of the screen height?

So i have created a persistent bottom sheet inside a Coordinator Layout.所以我在协调器布局中创建了一个持久的底部表。 I can set the height of LinearLayout to some value 500dp But what i want to acheive is set it's height to half of the screen.我可以将LinearLayout的高度设置为某个值500dp但我想要实现的是将其高度设置为屏幕的一半。 I think i have to get the screen height through DisplayMetrics and somehow apply it on LinearLayout我想我必须通过DisplayMetrics获取屏幕高度并以某种方式将其应用于LinearLayout


<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".post.PostActivity">

    <LinearLayout
        android:id="@+id/gallery_bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:layout_marginHorizontal="@dimen/margin_very_small"
        android:background="@drawable/background_sheet"
        android:orientation="vertical"
        app:behavior_peekHeight="@dimen/peek_height"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <View
            android:layout_width="@dimen/width_forty"
            android:layout_height="@dimen/divider_large"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/margin_small"
            android:layout_marginBottom="@dimen/margin_small"
            android:background="?android:listDivider" />

        <GridView
            android:id="@+id/gallery_grid_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:horizontalSpacing="@dimen/grid_spacing"
            android:nestedScrollingEnabled="true"
            android:numColumns="4"
            android:verticalSpacing="@dimen/grid_spacing" />

    </LinearLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int requiredHeight = height/2;
LinearLayout linearLayout = findViewById(R.id.gallery_bottom_sheet);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, requiredHeight);
linearLayout.setLayoutParams(layoutParams);

Just copy paste the above code inside your java file.只需将上述代码复制粘贴到您的 java 文件中即可。

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
LinearLayout linearLayout = findViewById(R.id.gallery_bottom_sheet);
LayoutParams params = layout.getLayoutParams();
params.height = (int) displayMetrics.heightPixels/2;
linearLayout.setLayoutParams(params);

Just copy paste the above code to your java file, inside "onCreate" method.只需将上面的代码复制粘贴到您的 java 文件中,在“onCreate”方法中。

First calculated the required screen height through DisplayMetrics首先通过DisplayMetrics计算出需要的屏幕高度

DisplayMetrics displayMetrics = new DisplayMetrics();

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

int halfScreenHeight = displayMetrics.heightPixels / 2;

Then get the getLayoutParams() of the existing linearLayout .然后获取现有linearLayoutgetLayoutParams() Set the height and applied.设置高度并应用。 (Casted it to CoordinatorLayout.LayoutParams as well) (也将其转换为CoordinatorLayout.LayoutParams

LinearLayout linearLayout = findViewById(R.id.gallery_bottom_sheet);

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) linearLayout.getLayoutParams();

params.height = halfScreenHeight;

linearLayout.setLayoutParams(params);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM