constraintlayout 滚动,如何在适合整个屏幕的ScrollView中将ConstraintLayout添加到LinearLayout中?...

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:

nbsil.png

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:

vyTWI.png

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值