Beware EditText on API 21

MENU

Beware EditText on API 21

12 OCTOBER 2015 on  android

Check out these two EditTexts. One is on an API 21 device, the other on an API 22 device.

See the difference? It's even more pronounced with "show layout bounds" enabled:

The height and vertical alignment of the EditTexts are different! This was caused by a change in the background of EditText between v21 and v22 (diff).

This change can cause sadness if your EditText is vertically aligned with other Views, such as this case in Trello:

The text should be aligned with the icons, yet clearly it's not. The screenshot above is from 5.0; any other version of Android looks perfectly fine.

This problem crops up even if you're using AppCompat. AppCompat usually defers to the system material styles on v21+, which is the source of the problem.

Solution

Both solutions I've come up with use resource qualifiers to handle API 21 in a special manner.

One possibility is to import your own EditText background assets for API 21. Unless your app is filled with vertically-aligned EditTexts this seems like more effort than it's worth, since precision-targeting the background of EditTexts for just a single API version is tricky.

The hackier (but easier) solution is to just define different margins or paddings based on the API level. For example, I found that they're ~6dp off, so you end up with resources like this:

<!-- values/dimens.xml -->  
<dimen name="edit_text_spacing">6dp</dimen>

<!-- values-v21/dimens.xml -->  
<dimen name="edit_text_spacing">0dp</dimen>

<!-- values-v22/dimens.xml -->  
<dimen name="edit_text_spacing">6dp</dimen>  

I'd be the first to admit it's ugly, but if there's only a handful of places you're fixing the problem, it's not so bad.

Dan Lew

Read more posts by this author.

Share this post
   
id="dsq-app1" name="dsq-app1" allowtransparency="true" frameborder="0" scrolling="no" tabindex="0" title="Disqus" width="100%" src="http://disqus.com/embed/comments/?base=default&version=535b1faff5d641b82126e27ee837ea19&f=danlewcodes&t_i=&t_u=http%3A%2F%2Fblog.danlew.net%2F2015%2F10%2F12%2Fbeware-edittext-on-api-21%2F&t_d=Beware%20EditText%20on%20API%2021&t_t=Beware%20EditText%20on%20API%2021&s_o=default" horizontalscrolling="no" verticalscrolling="no" style="display: block; margin: 1.75em 0px; width: 710px; border-style: none !important; border-width: initial !important; overflow: hidden !important; height: 689px !important;">

Using hardware layers to improve Android animation performance

When someone asks me about poor animation performance, one of the first questions I ask is whether or not…

Dan Lew Codes © 2015Proudly published with  Ghost
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码定义了两个结构体,分别是 nf_conn 和 nf_conntrack。 nf_conn 结构体包含了以下成员: - ct_general:一个 nf_conntrack 结构体,用于跟踪连接的一般信息。 - lock:自旋锁,用于保护对 nf_conn 结构体的并发访问。 - cpu:一个 16 位的无符号整数,表示该连接所在的 CPU 编号。 - zone:一个 nf_conntrack_zone 结构体,在 CONFIG_NF_CONNTRACK_ZONES 宏开启时有效。 - tuplehash:一个包含了 IP_CT_DIR_MAX 个元素的 nf_conntrack_tuple_hash 数组,用于存储连接的原始和回复的元组信息。 - status:一个无符号长整型数,用于表示连接的状态。 - timeout:一个 32 位的无符号整数,表示连接被认为已经死亡的时间戳(以 jiffies32 表示)。 - ct_net:一个 possible_net_t 类型的变量,表示连接所属的网络命名空间。 - nat_bysource:一个 hlist_node 结构体,在 CONFIG_NF_NAT 宏开启时有效。 - __nfct_init_offset:一个空结构体,用于初始化其他成员。 - master:一个指向 nf_conn 结构体的指针,表示该连接的期望连接(expectation)。 - mark:一个 32 位的无符号整数,在 CONFIG_NF_CONNTRACK_MARK 宏开启时有效。 - secmark:一个 32 位的无符号整数,用于安全标记,在 CONFIG_NF_CONNTRACK_SECMARK 宏开启时有效。 - ext:一个指向 nf_ct_ext 结构体的指针,表示连接的扩展信息。 - proto:一个联合体,用于存储其他模块保留的数据。 nf_conntrack 结构体包含了一个名为 use 的 atomic_t 类型成员,用于记录 nf_conntrack 结构体的使用计数。atomic_t 是一个原子类型,用于实现原子操作的计数器。 需要注意的是,这段代码只是结构体的定义,没有展示结构体成员的具体实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值