【发布时间】:2016-08-10 15:50:34
【问题描述】:
无法从我的 CustomView 获取重力属性 (android:gravity)。
XML
<MyCustomView
...
android:gravity="right"
/>
我的自定义视图;
class MyCustomView extends LinearLayout{
...
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
getGravity(); //Throws method not found exception
((LayoutParams)getLayoutParams()).gravity; //This returns the value of android:layout_gravity
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
...
}
getGravity(); throws method not found 异常;
((LayoutParams)getLayoutParams()).gravity;返回android:layout_gravity的值
无论如何我可以从视图中获取重力属性吗?
【问题讨论】:
-
首先您需要了解
gravity和layout_gravity不是一回事。 stackoverflow.com/documentation/android/94/layouts/398/… -
我知道兄弟。我认为你不清楚这个问题。我想从我的 java 代码中获取我在 XML 中放入的重力属性的值。不是布局重力。我可以得到 layout_gravity 但不能得到重力。
-
@BartekLipinski 我不认为他对这两个感到困惑,只是他能够得到一个,而不是另一个,并质疑他为什么不能
getGravity() -
我不知道这是否有效,但您似乎可以在较低的 API 上尝试这样的反射,但使用来自 android 24 的 getGravity。stackoverflow.com/a/38605915/4183017
-
ok..sry... 我删除了我的答案.. 'getGravity' 的东西适用于 >24,你可以按照这里的答案来实现它stackoverflow.com/questions/26558289/get-linearlayout-gravity
标签: android custom-view