【问题标题】:How to add button icons to ActionBar in Nativescript?如何在 Nativescript 中将按钮图标添加到 ActionBar?
【发布时间】:2016-10-10 14:40:08
【问题描述】:

大家好!

我正在尝试在我的 Nativescript 应用程序中的 ActionBar 中添加一个汉堡菜单图标,该图标会触发侧边抽屉,但我无法添加该菜单图标。

这是official documentation on the matter

到目前为止,这是我的.xml 代码:

<dpg:DrawerPage loaded="pageLoaded" navigatedTo="onNavigatingTo"  
xmlns:dpg="nativescript-telerik-ui/sidedrawer/drawerpage"
xmlns:drawer="nativescript-telerik-ui/sidedrawer"
xmlns:sdc="views/side-drawer-content"
xmlns="http://www.nativescript.org/tns.xsd">
<navigation.actionBar>
  <ActionBar title="Drawer Over Navigation">
    <android>
      <NavigationButton icon="res://ic_menu" tap="toggleDrawer" />
    </android>
    <ios>
      <ActionItem icon="res://ic_menu" ios.position="left" tap="toggleDrawer" />
    </ios>
  </ActionBar>
</navigation.actionBar>

<dpg:DrawerPage.sideDrawer>
<drawer:RadSideDrawer id="drawer">
  <drawer:RadSideDrawer.drawerContent>
    <sdc:side-drawer-content />
  </drawer:RadSideDrawer.drawerContent>
</drawer:RadSideDrawer> 
</dpg:DrawerPage.sideDrawer>

<StackLayout cssClass="mainContent">
    <Label text="{{ exampleText }}" textWrap="true" cssClass="drawerContentText"/>
    <Button text="Toggle Drawer" tap="toggleDrawer" icon="res://ic_menu" />
</StackLayout>
</dpg:DrawerPage>

我认为相关部分在这里,但我看不出我的错误可能是什么。

<ActionBar title="Drawer Over Navigation">
    <android>
      <NavigationButton icon="res://ic_menu" tap="toggleDrawer" />
    </android>
    <ios>
      <ActionItem icon="res://ic_menu" ios.position="left" tap="toggleDrawer" />
    </ios>
</ActionBar>

请告诉我我可以提供的任何其他信息,以使这个问题更清楚。

【问题讨论】:

    标签: android xml telerik nativescript


    【解决方案1】:

    选项 1。)

    文档假设您的资源文件夹中已经有图像 ic_menu(app/App_Resources/Android/drawables-xxx 用于 Android 和 app/App_Resources/iOS/Assets.xcassets )。例子可以找到here

    如果您没有此图像(针对不同设备进行了缩放),那么您应该提供它。这个概念与您对 AppIcons (article here) 所做的几乎相同。还有一些工具可以自动生成不同比例的图像 - 例如this one here

    选项 2。)

    注意:这仅适用于the syntax for custom ActionItems

    另一个适用的选项是使用 IconFonts 而不是图像创建汉堡菜单(必须针对不同的分辨率精确调整大小)

    例子:

    1.) Use this instead of image

    2.) 导入文件夹字体example here中的图标字体

    3.) 创建一个 CSS 类

    .font-awesome {
        font-family: "FontAwesome";
        font-size: 14;
        font-weight: normal;
        text-align: center;
    }
    

    4.) 为您要使用的字形应用代码(在本例中为汉堡菜单)

    <Button text="&#xf0c9;" class="font-awesome" tap="" />
    

    【讨论】:

    • 非常感谢您的回答!但是,当我按照您的示例进行操作时,我得到一个带有字面意思“”的按钮,而不是汉堡菜单的 unicode 字符 =O
    • 对于 android,font-family 属性必须是字体文件的名称(不带引号)。比如你有app/fonts/fontawesome.tff,那么在CSS中:font-family: fontawesome;
    • @DinhLe 我照你说的做了:fontawesome-webfont.ttf 是文件名,css 中的属性是font-family: fontawesome-webfont,但没有用 =(
    • CSS 文件中的字体名称应该是 FontAwesome,与示例应用程序中显示的完全一样 - 该名称是由字体作者创建的,而不是文件名跨度>
    • @NickIliev 是对的。我只是忘了添加“;”在文本值的末尾。谢谢你们的帮助! =D
    猜你喜欢
    • 1970-01-01
    • 2013-06-29
    • 2020-03-25
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多