Dificultad usando AttributeSet.getAttributeIntValue ()
Mi aplicación necesita la altura y el ancho del ImageView ampliado. Pensé que esto debería funcionar:
package my.android.test; public class TestImageView extends ImageView { private int mWidth=0; private int mHeight=0; public TestImageView(Context context, AttributeSet as) { super(context, as); final String xmlns="http://schemas.android.com/apk/res/android"; mWidth = as.getAttributeIntValue(xmlns, "layout_width", 0); mHeight = as.getAttributeIntValue(xmlns, "layout_height", 0); } @Override protected void onDraw(Canvas canvas) { //do stuff } }
Pero no funciona. 0 es devuelto por getAttributeIntValue () aunque el layout_height se define en la disposición XML:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true" android:background="#fffcb95a" <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content" <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="6dip" <my.android.test.TestImageView android:id="@+id/TestImageViewID" android:layout_width="100dip" android:layout_height="100dip" android:src="@drawable/icon" android:scaleType="centerCrop" /> </RelativeLayout </FrameLayout </LinearLayout </ScrollView
qué hacer ?
Sólo getWidth()
llamar a getWidth()
y getHeight()
.
Tenga en cuenta una explicación de por qué el intento original no funciona – layout_width y layout_height no son realmente la celebración de enteros, que son la celebración de las dimensiones. Usted necesita recuperar estos a través de Context / Theme.obtainStyledAttributes, por lo que puede obtenerlos como un TypedValue que se puede convertir en píxeles.