How to use text instead of image on Android Tabbed Page

时间:2016-12-28 22:24:07

标签: xamarin xamarin.android xamarin.forms

I'm trying to follow along with the TabbedPageWithNavigationPage sample as shown here: https://github.com/xamarin/xamarin-forms-samples/tree/master/Navigation/TabbedPageWithNavigationPage

However in their sample the Droid project does NOT have a settings.png page see: (https://github.com/xamarin/xamarin-forms-samples/blob/master/Navigation/TabbedPageWithNavigationPage/TabbedPageWithNavigationPage/SettingsPage.xaml)...yet the application gracefully falls back to text only.

In my version (building from scratch) if I do not include the settings.png file, the Droid version crashes with a missing resource error.

I'm sure I'm missing something fundamental...any thoughts on what it might be?

Update MainActivity.CS and App.xaml.cs:

    [Activity(Label = "AppZero.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);

        LoadApplication(new App());
    }
}
public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new AppZeroPage();
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }

Update 2 App.xaml

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="AppZero.App">
	<Application.Resources>
		<!-- Application resource dictionary -->
	</Application.Resources>
</Application>

Update 3 AppZeroPageCS.cs

using System;
using Xamarin.Forms;
namespace AppZero
{
    public class AppZeroPageCS:TabbedPage
    {
        public AppZeroPageCS()
        {
            Children.Add(new SettingsPage());
            Children.Add(new SettingsPage());
            Children.Add(new SettingsPage());
        }
    }
}

Update 4 SettingsPage.cs

using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace AppZero
{
    public partial class SettingsPage : ContentPage
    {
        public SettingsPage()
        {
            InitializeComponent();
        }
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="AppZero.SettingsPage" Icon="settings.png" Title="Settings">
	<ContentPage.Content>
		<StackLayout>
			<Label Text="Settings go here" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
		</StackLayout>
	</ContentPage.Content>
</ContentPage>

1 个答案:

答案 0 :(得分:0)

解释

您的应用崩溃了,因为您要在SettingsPage.xaml中分配图标:Icon="settings.png"。但是,您的Android项目的/Resources/Drawable文件夹中没有settings.png文件。

有两种方法可以解决这个问题:

  1. Icon="settings.png"移除SettingsPage.xaml
    • 这将仅使用Tab的文本并删除图像
  2. 将settings.png文件添加到Android项目的每个/Resources/Drawable文件夹中
    • 这会将图片添加到您的标签
  3. 修复1:删除Icon="settings.png"

    以下是排除SettingsPage.xaml属性的Icon的更新版本:

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="AppZero.SettingsPage" Title="Settings">
        <ContentPage.Content>
            <StackLayout>
                <Label Text="Settings go here" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    

    修复2:将Settings.png添加到/Resources/Drawable

    /Resources/内,有多个drawable个文件夹,每个文件夹都与不同的屏幕分辨率密度相关联。您需要创建多个不同分辨率的png个文件,并将具有适当分辨率的png文件添加到相应的文件夹中。

    Drawable Folders

    以下是选项卡式页面图标的分辨率:

    • 24×24(mdpi)
    • 36×36(hdpi)
    • 48×48(xhdpi)
    • 72×72(xxhdpi)
    • 96×96(xxxhdpi)

    例如,xhdpi png的分辨率应为48x48像素。

    Xamarin有关于Drawable文件夹的additional documentation