如何在适合整个屏幕的ScrollView中将ConstraintLayout添加到LinearLayout中? [英] How do you add a ConstraintLayout to a LinearLayout in a ScrollView that fits the whole screen?
问题描述
我希望能够滚动浏览LinearLayout,其中每个元素都是适合整个屏幕的ConstraintLayout.
I want to be able to scroll through a LinearLayout, where each element is a ConstraintLayout that fits the whole screen.
我在每个Layout中都尝试了layout_height的多个值,但似乎没有任何效果. 我可以在适合我的屏幕的layout_height中进行硬编码,但这并不理想.
I've tried multiple values for layout_height in each Layout but nothing seems to be working. I could hardcode in a layout_height that fits my screen but that is not ideal.
这是我目前拥有的:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</ScrollView>
这是产生的设计:
尽管设计看起来像我想要的样子,但我无法向下滚动以查看后续的ConstraintLayout.
although the design looks the way I want, I am unable to scroll down to see the succeeding ConstraintLayout.
非常感谢您的帮助.我已经在网上搜索过,并试图修复了几个小时而没有结果
any help is greatly appreciated. I've searched online and tried to fix this for hours with no results
推荐答案
非常简单.您不能在xml上执行此操作,而必须在代码中执行.下面的第一个create类:
Ok its very simple. You can't do that at xml, must do in code. The first create class below:
public class DisplayMetricsUtils {
private static DisplayMetricsUtils displayMetricsUtils;
private static DisplayMetrics displayMetrics;
private Context mContext;
public DisplayMetricsUtils(Context mContext) {
this.mContext = mContext;
displayMetrics = mContext.getResources().getDisplayMetrics();
}
public static DisplayMetricsUtils newInstance(Context mContext) {
if (displayMetricsUtils == null) {
displayMetricsUtils = new DisplayMetricsUtils(mContext);
}
return displayMetricsUtils;
}
public int widthPixel() {
return displayMetrics.widthPixels;
}
public int heightPixels() {
return displayMetrics.heightPixels;
}
public float dpWidth() {
return widthPixel() / displayMetrics.density;
}
public float dpHeight() {
return heightPixels() / displayMetrics.density;
}
public void setHeightForView(View view, int ratio) {
view.getLayoutParams().height = heightPixels() / ratio;
}
public void setWidthForView(View view, int ratio, int bonus) {
view.getLayoutParams().width = widthPixel() / ratio + bonus;
}
public void setWidthForView(View view, int edge) {
view.getLayoutParams().width = widthPixel() - edge;
}
public void setHeightForView(View view, int ratio, int bonus) {
view.getLayoutParams().height = heightPixels() / ratio + bonus;
}
public int getHeightOfView(View view) {
return view.getLayoutParams().height;
}
}
并使用此:
DisplayMetricsUtils displayMetricsUtils = DisplayMetricsUtils.newInstance(UserActivity.this);
displayMetricsUtils.setHeightForView("Your constaintlayout", 1);
这篇关于如何在适合整个屏幕的ScrollView中将ConstraintLayout添加到LinearLayout中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!