How do you add a ConstraintLayout to a LinearLayout in a ScrollView that fits the whole screen?

旧巷老猫 提交于 2020-01-25 10:20:09

问题


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:

<?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>

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


回答1:


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


来源:https://stackoverflow.com/questions/59316089/how-do-you-add-a-constraintlayout-to-a-linearlayout-in-a-scrollview-that-fits-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!