【问题标题】:Xamarin forms Android how We change Tabbed Page Icon SizeXamarin 如何改变标签页图标大小
【发布时间】:2017-06-12 15:31:52
【问题描述】:

Xamarin 如何改变标签页图标大小。我有使用 AppCompact 主题,我想在 Tabbar.axaml 中增加标签页图标大小

【问题讨论】:

    标签: xaml xamarin xamarin.ios xamarin.android xamarin.forms


    【解决方案1】:

    您可以在原生平台中创建自定义渲染器并更改图标的大小。实际上你可以覆盖整个标签的布局。

    例如,在PCL中首先创建一个继承自TabbedPage的类:

    public class MyTabbedPage : TabbedPage
    {
    }
    

    然后在 Android 项目中创建它的渲染器,例如:

    [assembly: ExportRenderer(typeof(MyTabbedPage), typeof(MyTabbedPageRenderer))]
    
    namespace YourNameSpace.Droid
    {
        public class MyTabbedPageRenderer : TabbedPageRenderer
        {
            protected override void SetTabIcon(TabLayout.Tab tab, FileImageSource icon)
            {
                base.SetTabIcon(tab, icon);
    
                tab.SetCustomView(Resource.Layout.mytablayout);
                var imageview = tab.CustomView.FindViewById<ImageView>(Resource.Id.icon);
                imageview.SetBackgroundDrawable(tab.Icon);
            }
        }
    }
    

    我创建的布局是这样的:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:scaleType="fitCenter"
            android:id="@+id/icon"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>
    

    如你所见,我直接在axml文件中设置大小。

    当你想使用这个自定义的TabbedPage,你可以例如这样的代码:

    <local:MyTabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:TabbedPageForms"
                 x:Class="TabbedPageForms.MainPage">
    
        <local:TodayPage Title="Today" Icon="hamburger.jpg" />
    
        <local:SchedulePage Title="Schedule" Icon="hamburger.jpg" />
    </local:MyTabbedPage>
    

    后面的代码:

    public partial class MainPage : MyTabbedPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }
    

    【讨论】:

    • 我怎样才能为 iOS 做同样的事情?
    猜你喜欢
    • 2018-03-12
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多