我需要一个建议...我想根据此视图的大小创建具有不同布局的自定义视图组。
示例:
两种观点:
我想要的是什么:
通常,我在构造函数中向自定义视图添加视图。但我不能在这里做,因为我不知道构造函数中B的高度。我只知道A的高度。所以请告诉我应该使用哪种回调方法...当B的高度已知时,我可以根据这个高度添加所有子视图。
或者,如果你知道任何其他方法......请告诉我......
非常感谢!
答案 0 :(得分:2)
您应该考虑将所有视图放入滚动视图中的垂直线性布局,或者更好:使用单个listView而不是布局和scrollViews。
原因是它会根据需要自动处理滚动,具体取决于屏幕上的可用空间和视图大小。
答案 1 :(得分:0)
您可以使用此代码获取主布局的实际高度。 然后,您可以在if-else或switch-case块中使用该高度来检查所需的条件。
public int getLayoutSize() {
// Get the layout id
final LinearLayout root = (LinearLayout) findViewById(R.id.mainroot);
final AtomicInteger layoutHeight = new AtomicInteger();
root.post(new Runnable() {
public void run() {
Rect rect = new Rect();
Window win = getWindow(); // Get the Window
win.getDecorView().getWindowVisibleDisplayFrame(rect);
// Get the height of Status Bar
int statusBarHeight = rect.top;
// Get the height occupied by the decoration contents
int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
// Calculate titleBarHeight by deducting statusBarHeight from contentViewTop
int titleBarHeight = contentViewTop - statusBarHeight;
Log.i("MY", "titleHeight = " + titleBarHeight + " statusHeight = " + statusBarHeight + " contentViewTop = " + contentViewTop);
// By now we got the height of titleBar & statusBar
// Now lets get the screen size
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = metrics.widthPixels;
Log.i("MY", "Actual Screen Height = " + screenHeight + " Width = " + screenWidth);
// Now calculate the height that our layout can be set
// If you know that your application doesn't have statusBar added, then don't add here also. Same applies to application bar also
layoutHeight.set(screenHeight - (titleBarHeight + statusBarHeight));
Log.i("MY", "Layout Height = " + layoutHeight);
// Lastly, set the height of the layout
FrameLayout.LayoutParams rootParams = (FrameLayout.LayoutParams)root.getLayoutParams();
rootParams.height = layoutHeight.get();
root.setLayoutParams(rootParams);
}
});
return layoutHeight.get();
}