BlackBerry - height of available screen excluding banner & title fields
I would like to know how much vertical real estate is available to my manager on a screen (without scrolling).
I would like to have this manager in a separate class, not in the Screen subclass. Ideally, my manager would be able to be placed on any screen, without any knowledge of the screen specifics being passed to it - this manager would be able to find out how much space has been left unused by the Screen's title / banner fields.
I can get the screen size with many methods. That is the easy part. But it does not take account of title / banner fields.
What I would like to know is how to find the part of the screen that is not covered by any title or banner fields? But I would like to have a general solution, that can be used in a separate class file, with no prior knowledge of the screen it is to be displayed on.
(workaround: I would have to tell my manager what the height of the title / banner fields was, and then I could use these heights, with 开发者_运维百科Display.getHeight()
to calculate the available space)
I have a separate class (call it CobiPainter
) which is able to be used by any manager or screen class - it implements one method which I have called paintBackground(Graphics g)
. I call this method from within any related paintBackground(Graphics g)
override of the screen whose background I want to paint.
In order to be able to be used by any MainScreen
, with minimal coupling & extra work, I have made it so that the CobiPainter
class is able to calculate how much screen is available, without needing to be given any knowledge of the title / banner / status fields.
The code I have used relies on the fact that the MainScreen
has an internal manager to which we normally add the fields. This manager knows the height that I am looking for.
This is the hack/code I used:
Screen activeScreen = UiApplication.getUiApplication().getActiveScreen();
if (activeScreen instanceof MainScreen)
{
// get available height excluding banner / title
XYRect xy = ((MainScreen) activeScreen).getMainManager()
.getExtent();
heightOfTitleAndBanner = xy.y; // <---- can be useful too
availableHeight = xy.height; // <---- this is what I wanted
}
This code will not work until the manager in question is displayed, since it relies on calling the getActiveScreen()
method. It also requires that the MainScreen
was created with the parameter Manager.NO_VERTICAL_SCROLL
to force it to take the screen's size (thanks to Himanshu).
There are three fieldmanager like-VerticalFieldManager,HorizontalFieldManager and FlowFieldManager which can be set with Non-Scrollable,vertically as well as Horizontally. You have to create an object for the Horizontal/Vertical FieldManager with the parameter (Manager.NO_VERTICAL_SCROLL|Manager.NO_HORIZONTAL_SCROLL)
But you have to customize it to fulfill your conditions.
If you want to know the title size then simply add the containt in the Horizontal/Vertical FieldManager and get its height or width using getPreferedHeight() and getPreferedWidth() respectivelly.
Here is rought code :
final LabelField field = new LabelField("Himanshu")
{
protected void layout(int width, int height)
{
super.layout(width, height);
// setExtent(360, 50);
};
public int getPreferredHeight()
{
return super.getPreferredHeight();
}
};
final LabelField field1 = new LabelField("Himanshu")
{
protected void layout(int width, int height)
{
super.layout(width, height);
// setExtent(360, 50);
};
public int getPreferredHeight()
{
return super.getPreferredHeight();
}
};
setTitle(field);
setStatus(field1);
VerticalFieldManager fieldManager = new VerticalFieldManager(Manager.USE_ALL_WIDTH|Manager.NO_VERTICAL_SCROLL)
{
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout(maxWidth, maxHeight);
this.setExtent(360, Display.getHeight()-(field.getPreferredHeight())-(field1.getPreferredHeight()));
}
};
fieldManager.setBackground(BackgroundFactory.createSolidBackground(Color.BLUE));
System.out.println("-------------"+field.getPreferredHeight());
add(fieldManager);
What I do is a kind of a workaround of system's inability to provide that information.
1) Extend all your screens from a BaseScreen class. 2) When setting the Banner (setBanner()) in that class store the instance of the banner you put in. Example:
public abstract class BaseScreen extends MainScreen
{
private StandardTitleBar titleBar;
... // some initializer body, i.e. constructor
titleBar = getStandardTitleBar();
setBanner(titleBar);
...
}
3) Expose the height/width of the set banner in your BaseScreen class
public int getBannerHeight()
{
if (null == titleBar)
return 0;
else
return titleBar.getHeight();
}
4) inside protected void sublayout(int maxWidth, int maxHeight) of your manager use:
int bannerHeight = ((BaseScreen) getScreen()).getBannerHeight();
int titleHeight = getScreen().getTitleBar().getHeight() // TODO: protect from null
In my experience, I also account for the Virtual Keyboard and manager's padding when necessary.
精彩评论