calling setCompoundDrawablesWithIntrinsicBounds on multiple views with the same background gives inconsistent sizes

一世执手 提交于 2019-12-07 14:09:01

问题


I've got a LinearLayout with two EditText descendants (not direct children). I call EditText.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.xyz,0) on each to put a drawable (the same drawable) on the right side of each of the EditTexts. The two edit texts are identical, except for their order in the ListView. However, the size of the drawable in the first EditText becomes larger than the second after adding the drawables. Specifically, the first EditText resizes to accomodate the intrinsic size of the drawable, whereas the second EditText does not (although it does grow some small amount).

Why do two of the same drawables appear differently in the two near-identical views?

Also, I could solve this problem by figuring how to make the drawable not force the EditText to resize. As you can see, I've followed the directions at https://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList regarding making the background scale, but that doesn't seem to do the trick.

the code:

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <TextView 
        style="@style/fieldLabel"
        android:text="phone number"/>
    <EditText
        android:id="@+id/contactPhoneField" 
        android:inputType="phone"
        android:layout_width="fill_parent"
        style="@style/editableField"/>

</LinearLayout>

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <TextView 
        style="@style/fieldLabel"
        android:text="e-mail address"/>
    <EditText
        android:id="@+id/contactEmailField" 
        android:inputType="textEmailAddress"
        android:layout_width="fill_parent"
        style="@style/editableField"/>

</LinearLayout>

the drawable:

    <?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>

        <shape android:shape="rectangle">

            <gradient 
                android:startColor="#FFD060" 
                android:endColor="#FFBB33"
                android:angle="270"/>
            <padding android:left="5dp" android:top="5dp"
                android:right="5dp" android:bottom="5dp" />
            <corners android:radius="5dp" />
            <stroke android:width="2dp" android:color="#FF8800"/>
        </shape>

    </item>
    <item android:drawable="@drawable/warn"/><!-- a 35x35 px png which is taller than the native height of an EditText-->
</layer-list>

then at some point I call:

contactPhoneField.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.error_background, 0);
contactEmailField.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.error_background, 0);

Additionally, every subsequent time I call EditText.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.xyz,0) (after calling it the first time) causes first EditText's size to toggle between matching the 2nd EditText and matching the drawable size!

来源:https://stackoverflow.com/questions/10592621/calling-setcompounddrawableswithintrinsicbounds-on-multiple-views-with-the-same

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