0

I have a simple layout with a TextView in it. I am trying to estimate its dimensions (height and width) at run-time. The code I use is as follows:

   Log.v(this.toString(), "Screen width = " + this.getResources().getDisplayMetrics().widthPixels + " and height = " + this.getResources().getDisplayMetrics().heightPixels);
        Log.v(this.toString(), "TV width = " + tv.getWidth() + " and height = " + tv.getHeight());
        Log.v(this.toString(), "TV raw width = " + tv.getMeasuredWidth() + " and raw height = " + tv.getMeasuredHeight());
        Log.v(this.toString(), "TV width from display manager = " + getWindowManager().getDefaultDisplay().getWidth() + " and height = " + getWindowManager().getDefaultDisplay().getHeight());
        tv.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        Log.v(this.toString(), "TV width from measurespec = " + tv.getMeasuredWidth() + " and height = " + tv.getMeasuredHeight());
        tv.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {
                //get view height and width here.
                Log.v(this.toString(), "Inside layout width and height measurer.");
                Log.v(this.toString(), "In layout listener, TV width = " + tv.getWidth() + " and height = " + tv.getHeight());
                tv.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                Log.v(this.toString(), "In layout listener, TV raw width = " + tv.getMeasuredWidth() + " and raw height = " + tv.getMeasuredHeight());
                Log.v(this.toString(), "TV width from display manager = " + getWindowManager().getDefaultDisplay().getWidth() + " and height = " + getWindowManager().getDefaultDisplay().getHeight());
                if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
                    tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            }
        });

The phone I am testing it on is a Samsung Galaxy S Duos. In the LogCat, the height of the view is returned as 1058 pixels, which is greater than the phone's height of 800 pixels! The LogCat output is:

   01-12 12:35:38.749: V/com.sriram.hellotts.HelloTTS@415f3ac8(18595): Screen width = 480 and height = 800
01-12 12:35:38.749: V/com.sriram.hellotts.HelloTTS@415f3ac8(18595): TV width = 0 and height = 0
01-12 12:35:38.749: V/com.sriram.hellotts.HelloTTS@415f3ac8(18595): TV raw width = 0 and raw height = 0
01-12 12:35:38.749: V/com.sriram.hellotts.HelloTTS@415f3ac8(18595): TV width from display manager = 480 and height = 800
01-12 12:35:38.759: V/com.sriram.hellotts.HelloTTS@415f3ac8(18595): TV width from measurespec = 48 and height = 32
01-12 12:35:38.929: V/com.sriram.hellotts.HelloTTS$1@41068c38(18595): In layout listener, TV width = 480 and height = 1112
01-12 12:35:38.959: V/com.sriram.hellotts.HelloTTS$1@41068c38(18595): In layout listener, TV raw width = 8518 and raw height = 59
01-12 12:35:38.959: V/com.sriram.hellotts.HelloTTS$1@41068c38(18595): TV width from display manager = 480 and height = 800  

The layout is:

<RelativeLayout 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=".HelloTTS" >

         <!--  Footer aligned with the bottom -->
   <include
        android:id="@+id/footerLayout" 
        layout="@layout/footer_ehtv_layout" />

    <include
        android:id="@+id/headerLayout" 
        layout="@layout/header_ehtv_layout" />

     <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true" />

    <Button
        android:id="@+id/submitText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_above="@id/seekbar"
        android:text="@string/submit" />

    <Button
        android:id="@+id/nextPage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/submitText"
        android:layout_alignBaseline="@id/submitText"
        android:text="Next Page" />

    <Button
        android:id="@+id/previousPage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/submitText"
        android:layout_toLeftOf="@id/submitText"
        android:text="Prev. Page" />

     <ScrollView
        android:id="@+id/scrl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/submitText"
        android:layout_below="@id/headerLayout">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

           <TextView
            android:id="@+id/tv"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:inputType="none"
            android:clickable="true"
            android:longClickable="true"
            android:textIsSelectable="true"
            android:scrollbars="vertical"
            android:textSize="15sp" />
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

My question(s):
1. How is this even possible?
2. What else can I do to ensure that the TextView height and width are returned correctly, that is, after the layout has been drawn. Further, the TextView will only be displayed in portrait mode.

4

1 回答 1

0

我试过这个样本。

 Display display = this.getWindowManager().getDefaultDisplay();
int height=display.getHeight(); //you can get height
int weight=display.getWidth(); //you can get width


                  if(display.getHeight()==480&&display.getWidth()==320){

                  }

                  else if(display.getHeight()==800&&display.getWidth()==480){


                      }             
                  else if(display.getHeight()==320&&display.getWidth()==240){

                     }
于 2013-11-12T12:25:32.540 回答