英文:
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
<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>
答案1
得分: 1
首先,通过DisplayMetrics
计算所需的屏幕高度:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int halfScreenHeight = displayMetrics.heightPixels / 2;
然后获取现有linearLayout
的getLayoutParams()
。设置高度并应用(同时将其强制转换为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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论