andriod 获取attrs_attrs.xml中android:gravity的正确格式?

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值