2012-01-08 17 views
1

私の高さと幅を取得する必要があります。ここで示した例をサブクラス化して表示し、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; 

マイActivityクラス:

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"> 

答えて

1

高さが0

あるので、私はあなたのビューにはコンテンツがありません、それは正常なことだと思います210

は、カスタムのTextViewにいくつかのテキストを追加しようと高さが

TextView dummyView = new MyView(this); 
dummyView.setText("Hello"); 

デバイスの画面サイズを変更します:http://developer.android.com/reference/android/util/DisplayMetrics.html

+0

は(私はStackOverflowのに新しいです)尽くします。しかし、私の問題の解決策は何ですか?これが起こったと言われる例はありませんか? – michaelsmith

+0

今は、高さはまだ0です。デバイスを回転させると幅が変わりますが、高さは変わりません。私はデバイス画面の寸法を取得する必要があります。私は正しいアプローチをしていますか? – michaelsmith

+0

DisplayMetricsクラスでデバイスの画面サイズを取得することができます – ChristopheCVB

関連する問題