2011-07-01 10 views

答えて

4

使用getWidth()onWindowFocusChangedでのListViewのgetHeight()方法は、幅と高さを取得します。

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 


    System.out.println("Width:" + listview.getWidth()); 
    System.out.println("Height:" + listview.getHeight()); 

} 
+0

私はこのコードを入れています。私は方法を作成すると思います。 – naresh

2

現在の幅または任意のビューの高さを取得するには、あなたがonLayoutChangeリスナー

public class MainActivity extends Activity implements OnLayoutChangeListener { 

    private View newView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     newView = getLayoutInflater().inflate(R.layout.main_activity, null); 
     newView.addOnLayoutChangeListener(this); 
     setContentView(newView); 
    } 

    public void onLayoutChange(View v, int left, int top, int right, 
      int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 
     //Here you can get size of you ListView, e.g: 
     mylistView.getWidth(); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     newView.removeOnLayoutChangeListener(this); 
    } 
} 

27Nov15を設定する必要があります - これは良い答えですが、私はあなたがあまりにもかかわらず、リソースを解放するべきだと思います。これはアクティビティにとってはそれほど重要ではないかもしれませんが、より頻繁に出て行く可能性があるフラグメントでも同じアプローチを使用できます。

関連する問題