kuix教程2:XML / CSS的接口设计

来源:互联网 发布:c语言加密程序 编辑:程序博客网 时间:2024/04/30 02:12

教程2:XML / CSS的接口设计

先进的接口设计,XML和样式表

In this second part of the tutorial, we suggest improving the user interface with a XML/CSS approach.在本教程的第二部分,我们建议改善与一个XML / CSS的用户界面的方法。 Note that we use this approach because it is more user-friendly but you can get exactly the same results with the Java approach.请注意,我们使用这种方法,因为它更方便用户,但你可以得到完全的Java方法相同的结果。

The goal of this section is to provide a title, a content and an exit key to our J2ME application.本节的目的是提供一个标题,内容和退出键我们的J2ME应用程序。

 

创建一个资源文件夹

Switch on the files view and follow the next screens to create a res folder at in your project. 文件切换的观点,并按照下一屏幕创建一个res文件夹在您的项目。 This manipulation permit to separate the java source from resource files.这种操纵许可证分开资源文件java源。

Create a resources folder

Create a resources folder

Create a resources folder

Now mark this new folder as a source folder.现在,这个标志作为源文件夹的新文件夹。 Open the project properties, select the Libraries and resources section and click on the Add folder button.打开项目属性,选择图书馆和资源部分和按钮点击添加文件夹 Brownse to your project directory and select the new res folder. Brownse到您的项目目录,选择文件夹的新的水库

Add res folder

首先,创建XML和CSS资源文件

In your project resource folder ("res" by default), create helloworld.xml and helloworld.css.在您的项目资源文件夹(“水库默认”),创建helloworld.xml和helloworld.css。

note: to improve organize your projects, fill free to create xml and css subfolders to host those files. 注:改善组织您的项目,填补自由地创建XML和CSS子举办这些文件。

Create resource files

编辑XML文档

Open the helloworld.xml file and add the following content:打开helloworld.xml文件,并添加以下内容:

<screen title="helloworld"> <container style="layout:inlinelayout(false,fill); align:center"> <text text="Hello World!" /> <picture src="logo_community.png" /> </container> </screen>

It will produce a screen with a title bar filled with the text "helloworld".它会产生与文本“的HelloWorld填写”标题栏的屏幕。 The content of this screen is a container with 2 elements: our "Hello World!"该屏幕的内容是一个2元素的容器:我们的“Hello World!” text and a picture.文字与图片。

note: the directory "/res/img" is in the default path for an image. 注意:目录“/水库/ IMG公司”的形象是一个默认的路径。

initDesktopContent()方法

Edit initDesktopContent() method in your HelloWorld midlet class to load interface from the XML file:编辑initDesktopContent()在你的HelloWorld类的方法来加载文件接口的MIDlet从XML:

 public void initDesktopContent(Desktop desktop) { // Load the content from the XML file with Kuix.loadScreen static method Screen screen = Kuix.loadScreen("helloworld.xml", null); // Set the application current screen screen.setCurrent(); }

Instead of creating the widgets in the canvas in Java source code, we load the content of the XML resource file.而不是建立在Java源代码,在画布上小部件,我们加载的XML资源文件的内容。 This file is parsed by an integrated XML parser that instanciate the widgets for you.此文件是由一个综合解析的XML解析器实例化你的部件。 Compared to the previous implementation, performances are somewhat reduce by the string parsing time.相较于以前的实现,表演,一定程度上降低了分析时间字符串。

note:a parser component has been implemented with Kutil since severalembedded Java virtual machines does not include SAX parser. 注:1分析器组成部分已实施的虚拟机与嵌入式Java库季尔,因为有几个不包括SAX解析器。

If you start the midlet again, you should get something like the following screen:如果您再次启动MIDlet的,你应该得到的东西。

自定义的CSS midlet的外观

Open helloworld.css and change the default styles:打开helloworld.css并更改默认样式:

 text { align: center; font-style: normal; color: #f19300; } screenTopbar text { color: white; padding: 1 2 1 2; } screenTopbar { font-style: bold; bg-color: #cccccc; border: 0 0 1 0; border-color: #f19300; } desktop { bg-color: #444447; }

This stylesheet uses a CSS structure but its syntax has been adapted to fit the needs of mobile applications.这一个CSS样式表使用结构,但它的语法已经进行了调整,以适应移动应用的需要。

note: the padding is expressed in pixel only (as the margin and the border). 注:填充仅体现在像素(作为保证金和边境)。 The values are respectively TOP, RIGHT, BOTTOM and LEFT. 这些值分别为上,右,底部和左侧。

note 2: the desktop widget is a special Kuix widget. 注2:在桌面 Widget是一个特殊的Kuix部件。 The desktop's instance is created by Kuix on startup and is the root of the widgets tree. 在桌面上的实例是创建Kuix在启动时,是树的根的小部件。 Only a screen could be added as a child of the desktop. 只有一个屏幕可以增加一条,作为一个桌面孩子的。

在启动时加载样式

To load your CSS stylesheet, you can use the initDesktopStyles() on your midlet.要加载你的CSS样式表,您可以使用在您的MIDlet的initDesktopStyles(。 This mandatory method, is automaticaly called by the KuixMidlet on startup before the initDesktopContent() one.这种强制性的方法,是automaticaly称为()一个由KuixMidlet在启动前initDesktopContent。

Add the following line to initDesktopStyles() :添加下面一行到initDesktopStyles():

 public void initDesktopStyles() { // Load the stylesheet from the CSS-like file with Kuix.loadCss static method //  note: a stylesheet is not associated with a screen but with the midlet //  note 2 : by default '/css/' folder is use to find the 'helloworld.css' file Kuix.loadCss("helloworld.css"); }

Run the midlet

添加一个菜单的MIDlet

We will now, add a menu to our midlet.我们现在,我们添加一个菜单midlet的。 Like any other widget, you can add a menu to your midlet very quickly.像任何其他部件,您可以添加一个菜单的MIDlet非常快。 Edit the XML file and add the following code in the screen tag.编辑XML文件并在屏幕上添加标签下面的代码。

 <screen title="helloworld"> [...] <screenFirstMenu>Exit</screenFirstMenu> <screenSecondMenu> more... <menuPopup> <menuItem> About </menuItem> <menuItem> Exit </menuItem> </menuPopup> </screenSecondMenu> </screen>

This code adds a bottomBar and 2 menus to the midlet.此代码添加bottomBar和2个菜单的MIDlet。 By default, menus are binded to the softkeys: on the left softkey, we add a single element menu with "Exit" label ( screenFirstMenu tag in the XML file).默认情况下,菜单与束缚的软键:左软键,我们添加一个XML文件)菜单与单个元素(screenFirstMenu标签在“退出”标签。 On the second softkey, we add a Popup Menu ( screenSecondMenu tag) that contains 2 items ("Exit" and "About").关于第二个功能键,我们添加一个弹出菜单(screenSecondMenu标签),其中包含2项(“退出”和“关于”)。

note:Kuix handles by default the navigation across the focusable widgetsdisplayed on the screen (as menu items, buttons, textfields, etc.). 注:Kuix处理默认的部件的可聚焦在导航屏幕上显示(如菜单项,按钮,文本框等)。

自定义菜单的外观

Actually, if you start the midlet without setting any additional style, you may not recognize the menu bar.其实,如果你启动时不设置任何附加风格的MIDlet,你可能无法识别的菜单栏。 Let's set before, some additionnal lines in the stylesheet.让我们之前设置,在样式表中的一些additionnal线。

 screenBottombar { bg-color: #77787a; border: 1 0 0 0; border-color: #f19300; padding: 2 2 1 2; } screenBottombar text { color: white; } menuPopup { bg-color: #444447; border: 1; border-color: #838386; padding: 1; } menuItem { padding: 1 6 1 6; margin: 1 0 0 0; border: 1; border-color: #77787a; } menuItem:hover { border-color: #f19300; }

Add the previous code to your CSS file.前面的代码添加到你的CSS文件。 Thefirst block assigns a style to the bottom bar that is implicitelycreated when you add a menu: this space is reserved for the menu(s).第一个块分配一个酒吧风格,底部是implicitely创建当您添加一个菜单:这个空间是为菜单(第保留)。 The second section override the text color inside the bottom bar.第二部分覆盖内的底部栏文本的颜色。 The Popup Menu is shown on the right softkey and contains the Menu Items .弹出菜单显示在右软键,并包含菜单项 If you do not set a menuItem:hover property, you will not be able to identify which item is currently selected in you Popup Menu.如果您没有设置一个菜单项:悬停属性,你将无法确定哪些项目是目前您选择在弹出菜单。

启动

Run the midlet

Your menu is ready, but not very useful since we did not set any action on it.您的菜单准备好了,但不是非常有用,因为我们没有设置任何行动。 Next section will teach you how to add user actions (exit and about) to your application.下一节将教你如何添加用户操作到您的应用程序。

原创粉丝点击