问题描述
我正在使用 Xamarin.android。
建立一个标签页。并使用一个图标作为每个工作表的标题,如下所示。
但是图像比面积小得多。
有没有可能让它变大?
解决方法
建立一个标签页。并使用一个图标作为每个工作表的标题,如下所示。但图像比面积小得多。是否可以将其放大?
如果你想让TabbedPage图标变大,你必须自己制作一个自定义渲染器来实现。
首先,在 Android 平台中创建一个自定义渲染器 MyTabbedPageRenderer.cs
。
[assembly: ExportRenderer(typeof(TabbedPage),typeof(MyTabbedPageRenderer))]
namespace FormsSample.Droid
{
public class MyTabbedPageRenderer : TabbedPageRenderer
{
public MyTabbedPageRenderer(Context context) : base(context)
{
}
protected override void SetTabIcon(TabLayout.Tab tab,FileImageSource icon)
{
base.SetTabIcon(tab,icon);
tab.SetCustomView(Resource.Layout.Custom_tab_layou);
var imageview = tab.CustomView.FindViewById<ImageView>(Resource.Id.icon);
var textview = tab.CustomView.FindViewById<TextView>(Resource.Id.tv);
textview.Text = tab.Text;
imageview.SetBackgroundDrawable(tab.Icon);
}
}
}
然后为 TabbedPage 选项卡创建自定义选项卡,添加 ImageView 和 TevtView 以显示 TabbedPage 的图标和标题。您可以使 ImageView layout_height 更大以显示图标。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:orientation="vertical">
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="11dp"
android:textColor="#00FF6F" />
这是截图:
标签页:
<TabbedPage
x:Class="FormsSample.tabbedpage.TabbedPage5"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<!-- Pages can be added as references or inline -->
<ContentPage Title="Tab 1" IconImageSource="favorite.png" />
<ContentPage Title="Tab 2" IconImageSource="check.png" />
<ContentPage Title="Tab 3" IconImageSource="flag.png" />