我有一个简单的布局,里面有TextView
。我试图在运行时估计它的尺寸(高度和宽度)。我使用的代码如下:
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);
}
}
});
我正在测试的手机是Samsung Galaxy S Duos
。在LogCat
中,视图的高度返回为1058像素,大于手机的800像素高度! LogCat
输出为:
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
布局是:
<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>
我的问题:
1.这怎么可能呢?
2.我还能做些什么来确保正确返回TextView
高度和宽度,也就是说,在绘制布局之后。
此外,TextView
仅以纵向模式显示。
答案 0 :(得分: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){
}