I want to be able to scroll through a LinearLayout, where each element is a ConstraintLayout that fits the whole screen.
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.
Here's what I currently have:
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">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent">
Here's the design this produces:
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
解决方案
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;
}
}
And use this:
DisplayMetricsUtils displayMetricsUtils = DisplayMetricsUtils.newInstance(UserActivity.this);
displayMetricsUtils.setHeightForView("Your constaintlayout", 1);