ViewStub Layout Root Fields

LinearLayout has no fields after the ViewStub build. After bloating, can I only set fields programmatically, or is there another way?

+6
source share
2 answers

I came across the same question, but with RelativeLayout. I fixed it by putting fields in the ViewStub definition. This may not be the best way to do this, the best way may be to have a hierarchical layout, where the first child of the root layout is the one that has the fields set and contains all the other elements.

Not sure about the root cause of this, but these are two workarounds that I know of.

+6
source

Method 1: According to the Android developer website, whenever you use ViewStub, its layout settings are passed to the inflated child device. Therefore, to set the layout parameters, such as marginLeft, marginTop, marginRight, marginBottom, you must set the values โ€‹โ€‹in the ViewStub, which will be transferred to inflated children.

enter image description here

Method 2: otherwise, after the ViewStub becomes visible, you can dynamically create LayoutParams and set it to the ViewGroup. This also works.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.leftMargin =getResources().getDimensionPixelSize(R.dimen.four); params.rightMargin=getResources().getDimensionPixelSize(R.dimen.four); params.bottomMargin=getResources().getDimensionPixelSize(R.dimen.eight); findViewById(R.id.linearLayout).setLayoutParams(params); 
+2
source

Source: https://habr.com/ru/post/955762/


All Articles