Setting Layout parameters in android

后端 未结 2 1595
情深已故
情深已故 2020-12-14 02:55

Working with the XML file was easy as I could specify the parameters as


         


        
2条回答
  •  遥遥无期
    2020-12-14 03:31

    Depending on how many views you want to change the layouts on, I think it's better to create a helper method and pass whatever views you want to change to the method along with the height and width values you want them to change to:

    public void setWidthHeight(View v, int width, int height){
        LayoutParams lp;
        lp = v.getLayoutParams();
        lp.width = width;
        lp.height = height;
        v.setLayoutParams(lp);
    }
    

    Remember that setting the width and height here are not going to match the same values in your xml, i.e., android:layout_width="32dp" is not the same as lp.width = 32;

    Also, the LayoutParams type variable called lp should be of the type returned by your view... Check what type is returned by the view and import that type in your import statements.

提交回复
热议问题