Android drawable appears stretched for API <23 and correctly for API 23 and above

倾然丶 夕夏残阳落幕 提交于 2021-02-08 05:08:01

问题


I am currently using a list selector for my listview and created it using layer-list. This is my xml code:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/menu_arrow"
    android:width="18dp"
    android:height="36dp"
    android:top="5dp"/>

<item android:top="0dp" android:left="5dp" android:bottom="0dp" android:right="0dp">
    <shape android:shape="rectangle">
        <solid android:color="@android:color/transparent"/>
    </shape>
</item>

When i build the app, it appears fine on api 23 and above.

But for api 22 and below, it looks like this:

I came across this post Shape drawable in layer-list rendered incorrectly for APIs below 23 and understood that the attributes width and height work only for api 23 and above. So i added the width and height inside the size tag like this :

 <item android:drawable="@drawable/menu_arrow"
    android:top="5dp">
    <size android:height="36dp" android:width="18dp"/>
    </item>

But the image still appears stretched. What attribute is needed for api 22 and below to make the drawable look uniform for all the versions?


回答1:


I managed to solve it in the following way :

<item android:drawable ="@drawable/menu_arrow"
    android:right="285dp"
    android:top="5dp"
    android:bottom="10dp"/>

I guess for api < 23 we need to resize the drawable using top,right,bottom values and for api > 23 we can use width and height.



来源:https://stackoverflow.com/questions/45686116/android-drawable-appears-stretched-for-api-23-and-correctly-for-api-23-and-abov

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!