Android API23以下如何修改渐变Drawable(单形状LayerList)的高度
解决Android Layer-List固定渐变区域高度(兼容API23以下)
问题原因
API23及以下版本中,<item>的android:height属性不生效;而<shape>里的<size>仅指定绘制的原始尺寸,当父布局空间大于该尺寸时,shape会被拉伸,所以无法固定高度。
可行解决方案
使用<inset>标签包裹<shape>,通过设置超大的insetTop将渐变区域限制在底部4dp高度,同时保留圆角和渐变效果:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:gravity="bottom"> <!-- insetTop设为最大可用值,确保仅显示底部4dp区域 --> <inset android:insetTop="2147483647dp" android:insetBottom="0dp"> <shape android:shape="rectangle"> <size android:height="4dp"/> <corners android:bottomLeftRadius="0dp" android:topLeftRadius="0dp" android:bottomRightRadius="360dp" android:topRightRadius="360dp"/> <gradient android:startColor="#your_start_color" android:endColor="#your_end_color"/> </shape> </inset> </item> </layer-list>
原理说明
<inset>会给内部的shape添加内边距,超大的insetTop会把shape的顶部“推”到父布局的底部位置,结合shape自身的4dp高度,最终只会显示底部4dp的渐变区域,且在所有API版本中都能生效。
内容的提问来源于stack exchange,提问作者Albert Buigues
相关产品推荐
相关产品推荐

