I have the following situation:
in styles.xml:
?fooView.padding
?fooView.background
?fooView.gravity
in attrs.xml:
in themes.xml:
2dip
#AA000000
right|bottom
The problem is that I cannot figure out what the format for the fooView.gravity should be. I've already tried with string, enum and flag but none seem to work: I always get a java.lang.NumberFormatException: unable to parse 'right|bottom' as integer as soon as the view that uses this theme gets loaded.
All answers are appreciated.
解决方案
Those are the gravity values used by Android. You can use that in your attrs.xml:
In your layout XML you can use it this way:
custom:gravity="center|bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
And in Java code you can read the value using this:
int gravity = a.getInt(R.styleable.MyCustomView_gravity, Gravity.NO_GRAVITY);
and directly set it to a sub view, if that makes sense for you:
someSubView.setGravity(gravity);
You can look up those gravity values in the source of android.view.Gravity or here