Android : Android: XML size does not match dimension size

on Thursday, July 31, 2014


In my layout folder, I have the following main XML:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ImageView
android:id="@+id/img"
android:layout_height="@dimen/dimension"
android:layout_width="match_parent"
android:src="@drawable/ic_launcher"/>

</LinearLayout>




The problem is the height is not what I would expect it do be when I run the program. My values/dimens.xml specifies what the actual height is:



<resources>
<dimen name="dimension">32dp</dimen>
</resources>




And in my code, when I view the height ImageView height in pixels and the dimension size in pixels, they differ:



View v = findViewById(R.id.img);
Log.e("Image height in px", "" + v.getHeight());//64

Resources r = getResources();
Log.e("Dimension in DP)", "" + r.getDimension(R.dimen.dimension));//64
int dimension = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, r.getDimension(R.dimen.dimension), r.getDisplayMetrics());
Log.e("Dimension in PX", "" + dimension); //128




Why isn't the ImageView the same height as the calculated height?


0 comments:

Post a Comment