1
我需要得到我的视图的高度和宽度,我已经遵循这里给出的例子与子类View和使用onSizeChanged(...)。 但是很奇怪的是,onSizeChanged给了我正确的宽度,但高度始终为0。这里是我的子类:onSizeChanged返回宽度,但不是高度
public class MyView extends TextView {
int xMax=0; int yMax=0;Context c;
public MyView(Context context) {
super(context);
this.c=context;
}
@Override
public void onSizeChanged(int w, int h, int oldW, int oldH) {
xMax = w;
yMax = h;
我的动态类:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView dummyView = new MyView(this);
LinearLayout ll = (LinearLayout) findViewById(R.id.mainmtc);
ll.addView(dummyView);
和我的XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainmtc" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1"
androiad:orientation="vertical">
会做(我是新来的StackOverflow)。但是,我的问题的解决方案是什么?所有发现的例子从未提及过这种情况? – michaelsmith 2012-01-08 12:18:39
现在这样做了,但高度仍然是0.当旋转设备时,我改变宽度,但不是高度。我需要获取设备屏幕的尺寸。我采取了正确的方法吗? – michaelsmith 2012-01-08 12:26:28
设备屏幕尺寸可以通过DisplayMetrics类检索 – ChristopheCVB 2012-01-08 12:27:54