Blackberry 场中心的场中心内容
我做了一个水平场马安格,它将占据整个屏幕的宽度和10%的高度。现在我想分别向这个管理器添加3个字段。第一个是位图字段(屏幕宽度的10%),第二个是LabelField(屏幕宽度的80%),第三个是位图字段(屏幕宽度的10%) 剩下的全部工作就是将这些字段的内容集中到字段本身的中心 以下是代码:-Blackberry 场中心的场中心内容,blackberry,Blackberry,我做了一个水平场马安格,它将占据整个屏幕的宽度和10%的高度。现在我想分别向这个管理器添加3个字段。第一个是位图字段(屏幕宽度的10%),第二个是LabelField(屏幕宽度的80%),第三个是位图字段(屏幕宽度的10%) 剩下的全部工作就是将这些字段的内容集中到字段本身的中心 以下是代码:- public final class MyScreen extends MainScreen { public MyScreen() { final int disWidth = Disp
public final class MyScreen extends MainScreen
{
public MyScreen()
{
final int disWidth = Display.getWidth();
final int disHeight = Display.getHeight();
final int heightHFM = (disHeight*10)/100;
HorizontalFieldManager hfm = new HorizontalFieldManager(){
protected void sublayout(int maxWidth, int maxHeight) {
// TODO Auto-generated method stub
super.sublayout(maxWidth, maxHeight);
this.setExtent(disWidth,heightHFM);
}
};
hfm.setBackground(BackgroundFactory.createSolidBackground(Color.GRAY));
BitmapField bmp_leftArrow = new BitmapField(Bitmap.getBitmapResource("arrow_left.png")){
protected void layout(int width, int height) {
// TODO Auto-generated method stub
super.layout(width, height);
this.setExtent((disWidth*10)/100, height);
}
};
bmp_leftArrow.setBackground(BackgroundFactory.createSolidBackground(Color.BLUE));
LabelField lbl_tit = new LabelField("Current Date"){
protected void layout(int width, int height) {
// TODO Auto-generated method stub
super.layout(width, height);
this.setExtent((disWidth*80)/100, height);
}
};
lbl_tit.setBackground(BackgroundFactory.createSolidBackground(Color.RED));
BitmapField bmp_rightArrow = new BitmapField(Bitmap.getBitmapResource("arrow_left.png")){
protected void layout(int width, int height) {
// TODO Auto-generated method stub
super.layout(width, height);
this.setExtent((disWidth*10)/100, height);
}
};
bmp_rightArrow.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));
hfm.add(bmp_leftArrow);
hfm.add(lbl_tit);
hfm.add(bmp_rightArrow);
add(hfm);
}
}
当我们在android中使用任何视图的重力时,需要实现相同的输出,而不是将
LabelField
(变量lbl_tit
)直接添加到HorizontalFieldManager
中,首先将其添加到VerticalFieldManager
。然后将VerticalFieldManager
添加到您的HorizontalFieldManager
创建LabelField
时,设置字段.Field\u HCENTER
样式位,使其在垂直字段管理器中居中对齐
这是我的密码:
public final class MyScreen extends MainScreen
{
public MyScreen()
{
final int disWidth = Display.getWidth();
final int disHeight = Display.getHeight();
final int heightHFM = (disHeight*10)/100;
HorizontalFieldManager hfm = new HorizontalFieldManager(){
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
this.setExtent(disWidth,heightHFM);
}
};
hfm.setBackground(BackgroundFactory.createSolidBackground(Color.GRAY));
BitmapField bmp_leftArrow = new BitmapField(Bitmap.getBitmapResource("arrow_left.png")){
protected void layout(int width, int height) {
super.layout(width, height);
this.setExtent((disWidth*10)/100, height);
}
};
bmp_leftArrow.setBackground(BackgroundFactory.createSolidBackground(Color.BLUE));
VerticalFieldManager vfm = new VerticalFieldManager(USE_ALL_WIDTH){
protected void sublayout(int width, int height) {
super.sublayout((disWidth*80)/100, height);
this.setExtent((disWidth*80)/100, height);
}
};
vfm.setBackground(BackgroundFactory.createSolidBackground(Color.RED));
LabelField lbl_tit = new LabelField("Current Date", Field.FIELD_HCENTER);
vfm.add(lbl_tit);
BitmapField bmp_rightArrow = new BitmapField(Bitmap.getBitmapResource("arrow_left.png")){
protected void layout(int width, int height) {
super.layout(width, height);
this.setExtent((disWidth*10)/100, height);
}
};
bmp_rightArrow.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));
hfm.add(bmp_leftArrow);
hfm.add(vfm);
hfm.add(bmp_rightArrow);
add(hfm);
}
}
从LabelField中删除重写的布局
方法,您不再需要它了。应该是这样的:
对于子字段的自定义定位和对齐,具有指定布局和字段定位的专用FieldManager非常有用。您可以尝试以下CustomHorizontalFieldManager
扩展Manager
生成输出
使用管理器
public类MyScreen扩展主屏幕
{
公共MyScreen()
{
CustomHorizontalFieldManager chfm=新CustomHorizontalFieldManager();
Background bg=BackgroundFactory.createSolidBackground(颜色:棕色);
退步地(bg);
BitmapField bmf0=新的BitmapField(Bitmap.getBitmapResource(“img0.png”);
BitmapField bmf1=新的BitmapField(Bitmap.getBitmapResource(“img1.png”);
CutomLabelField lblf=新CutomLabelField(“当前日期”);
bg=BackgroundFactory.createSolidBackground(颜色.绿色);
lblf.退步背景(bg);
bg=BackgroundFactory.createSolidBackground(颜色.绿色);
bmf0.退避地(bg);
bg=BackgroundFactory.createSolidBackground(颜色.绿色);
bmf1.退根地(bg);
chfm.add(bmf0);
chfm.add(lblf);
chfm.add(bmf1);
加(瑞士法郎);
}
}
管理者的实施
类CustomHorizontalFieldManager扩展管理器{
公共CustomHorizontalFieldManager(){
超级(0);
}
私有整数宽度0、宽度1、宽度2;
专用字段f0、f1、f2;
int x0,x1,x2,y0,y1,y2;
受保护的空位子布局(整数宽度、整数高度){
如果(getFieldCount()==3){
//计算总宽度、总高度
宽度=Display.getWidth();
高度=getPercentage(Display.getHeight(),10);
//计算字段的宽度
宽度0=getPercentage(宽度,10);
宽度1=getPercentage(宽度,80);
宽度2=百分比(宽度,10);
f0=getField(0);
f1=getField(1);
f2=getField(2);
//布局所有子对象
布局子对象(f0,宽度0,高度);
布局子对象(f1,宽度1,高度);
布局子对象(f2,宽度2,高度);
//指定子对象的位置。
//可以在此处调整字段的对齐方式。
//下面的行将对齐它们
//在中心,垂直和水平。
x0=(width0-f0.getWidth())/2;
x1=width0+(width1-f1.getWidth())/2;
x2=width0+width1+(width2-f2.getWidth())/2;
y0=(height-f0.getHeight())/2;
y1=(height-f1.getHeight())/2;
y2=(height-f2.getHeight())/2;
setPositionChild(f0,x0,y0);
setPositionChild(f1,x1,y1);
setPositionChild(f2,x2,y2);
设置范围(宽度、高度);
}否则{
//经理有一些建议
//字段数无效。
//让经理隐形。
setExtent(0,0);
}
}
int getPercentage(int值,int百分比){
返回值(值*百分比)/100;
}
}
CustomLabelField的实现草案
类CutomLabelField扩展字段{
私有字符串文本;
公共CutomLabelField(字符串文本){
this.text=(text==null)?“”:text;
}
int-xText;
int yText;
int可用宽度;
受保护的空白布局(整数宽度、整数高度){
//定位文本。
int textWidth=getFont().getAdvance(文本);
availableWidth=宽度-getPaddingLeft()-getPaddingRight();
如果(可用宽度<文本宽度){
xText=getPaddingLeft();
}否则{
xText=getPaddingLeft()+(可用宽度-textWidth)/2;
}
yText=(height-getFont().getHeight())/2;
//使用所有宽度和高度
设置范围(宽度、高度);
}
受保护的空心漆(图形){
//设置文本颜色
图形.设置颜色(颜色.黑色);
graphics.drawText(text,xText,yText,DrawStyle.省略号,可用宽度);
}
公共void setText(字符串文本){
this.text=(text==null)?“”:text;
updateLayout();
}
公共字符串getText(){
返回此.text;
}
}
我按你说的做了尝试,但结果相同:-VerticalFieldManager VerticalFieldManager=新的VerticalFieldManager();LabelField lbl_tit=新LabelField(“当前日期”,Field.Field_HCENTER){受保护的无效布局(int-width,int-height){//TODO自动生成的方法存根super.layout(width,height);this.setExtent((disWidth*80)/100,height);};lbl_tit.setBackground(BackgroundFactory.createSolidBackground(Color.RED));垂直的