将材料设计的持久性底部抽屉的高度设置为屏幕高度的一半。

huangapple go评论85阅读模式
英文:

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

问题

<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="wrap_content"
        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>
英文:

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. I think i have to get the screen height through DisplayMetrics and somehow apply it on LinearLayout


&lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.post.PostActivity&quot;&gt;

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

        &lt;View
            android:layout_width=&quot;@dimen/width_forty&quot;
            android:layout_height=&quot;@dimen/divider_large&quot;
            android:layout_gravity=&quot;center_horizontal&quot;
            android:layout_marginTop=&quot;@dimen/margin_small&quot;
            android:layout_marginBottom=&quot;@dimen/margin_small&quot;
            android:background=&quot;?android:listDivider&quot; /&gt;

        &lt;GridView
            android:id=&quot;@+id/gallery_grid_view&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            android:horizontalSpacing=&quot;@dimen/grid_spacing&quot;
            android:nestedScrollingEnabled=&quot;true&quot;
            android:numColumns=&quot;4&quot;
            android:verticalSpacing=&quot;@dimen/grid_spacing&quot; /&gt;

    &lt;/LinearLayout&gt;


&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

答案1

得分: 1

首先,通过DisplayMetrics计算所需的屏幕高度:

DisplayMetrics displayMetrics = new DisplayMetrics();

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

int halfScreenHeight = displayMetrics.heightPixels / 2;

然后获取现有linearLayoutgetLayoutParams()。设置高度并应用(同时将其强制转换为CoordinatorLayout.LayoutParams):

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

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

params.height = halfScreenHeight;

linearLayout.setLayoutParams(params);
英文:

First calculated the required screen height through DisplayMetrics

DisplayMetrics displayMetrics = new DisplayMetrics();

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

int halfScreenHeight = displayMetrics.heightPixels / 2;

Then get the getLayoutParams() of the existing linearLayout. Set the height and applied. (Casted it to CoordinatorLayout.LayoutParams as well)

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

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

params.height = halfScreenHeight;

linearLayout.setLayoutParams(params);

答案2

得分: 0

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);
英文:
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.

答案3

得分: 0

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

请将以上代码复制粘贴到您的Java文件中,在 "onCreate" 方法内部。

英文:
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.

huangapple
  • 本文由 发表于 2020年5月4日 03:16:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61580283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定