Java method for android:layout_gravity

前端 未结 6 2058
别跟我提以往
别跟我提以往 2020-12-03 21:07

I would like to know if is there a way to call android:layout_gravity property from a Java method. I didn\'t found any method in Android documentation to do it.

6条回答
  •  感动是毒
    2020-12-03 21:26

    Watchout! This wouldn't work with LinearLayout though. Because LinearLayout.LayoutParams(...) constructor is different from FrameLayout.LayoutParams(...) constructor. The third parameter is not gravity but weight.

         LinearLayout.LayoutParams(int width, int height, float weight)
    

    as opposed to

         FrameLayout.LayoutParams(int width, int height, int gravity)
    

    and this call, despite not producing compiler error is actually wrong:

    lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.LEFT);
    

提交回复
热议问题