0

I have a ListView where the rows have a TextView. In my code I need to know the height of each row to be able to dynamically calculate the list size. This is the layout used in each row:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title_subgroup"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="5dp"
    android:textSize="18sp"
    android:textStyle="bold" />

This is the code I use to get the size in pixels:

    float sizeInDip = 10f;
    float sizeInSp = 18f;
    ROW_HEIGHT = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeInDip, getResources().getDisplayMetrics())
            + context.getResources().getDisplayMetrics().scaledDensity * sizeInSp);

The result I got is lower than the real size. With this code ROW_HEIGHT is 36, and it should be something like 45. Why is that difference?

Thanks!

4

1 に答える 1

1

myView.getParent()を呼び出すことでビューの親をgetWidth()取得getHeight()でき、必要なピクセルを取得できます。ドキュメントは、このトピックに関する詳細情報を提供しています http://developer.android.com/reference/android/view/View.html

于 2013-02-27T08:40:01.647 に答える