在我的应用程序中,我想使用适配器来显示列表视图。但是我想获取列表视图的当前高度和宽度(即在使用适配器生成列表之后)。如何获取?有人能帮忙吗?
谢谢。
谢谢。
在onWindowFocusChanged
中使用ListView的getWidth()
和getHeight()
方法来获取宽度和高度。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
System.out.println("Width:" + listview.getWidth());
System.out.println("Height:" + listview.getHeight());
}
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-这是一个很好的答案,我认为你应该释放资源。虽然对于Activity来说可能不那么重要,但是相同的方法可以用于Fragment,因为它们可能更频繁地出现和消失。