Get LinearLayout Gravity

后端 未结 3 1474
一整个雨季
一整个雨季 2020-12-21 07:13

I was needing to get the value of the gravity of a LinearLayout. I checked the documentation and I only found how to set it ( setGravity(value) ).

3条回答
  •  被撕碎了的回忆
    2020-12-21 07:26

    API >= 24

    getGravity ()

    https://developer.android.com/reference/android/widget/LinearLayout.html#getGravity()

    API < 24

    using reflection:

    int gravity = -1;
    
    try {
        final Field staticField = LinearLayout.class.getDeclaredField("mGravity");
        staticField.setAccessible(true);
        gravity =  staticField.getInt(linearLayout);
    
        //Log.i("onFinishInflate", gravity+"");
    } 
    catch (NoSuchFieldException e) {} 
    catch (IllegalArgumentException e) {}
    catch (IllegalAccessException e) {}
    

提交回复
热议问题