如何从渲染器在 ToolbarItem 的中心添加自定义视图 [英] How to add custom view in Center of ToolbarItem from Renderer
问题描述
我正在从 NavigationPageRenderer for android 在 ToolbarItem 的中心添加自定义视图.但它似乎不起作用.在下面查看我的代码
公共类 CustomNavigationPageRenderer : NavigationPageRenderer{公共覆盖无效 OnViewAdded(Android.Views.View child){base.OnViewAdded(child);var lastPageElement?.Navigation?.NavigationStack?.Last().ToString();if (lastPage == "ParentDashboardPage"){LayoutInflater li = LayoutInflater.From(Context);Android.Views.View customView = li.Inflate(Resource.Layout.customActionbar, null);var _toolbar = FindViewById(Resource.Id.toolbar);_toolbar.RemoveAllViews();//_toolbar.AddView(text,new Toolbar.LayoutParams(GravityFlags.Center));即使这不起作用_toolbar.AddView(customView);}}}
customActionbar.axml 文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"><文本视图android:layout_width="wrap_content"机器人:文本=仪表板"android:textColor="@color/white"机器人:layout_gravity="中心"android:id="@+id/tvActionBarTitle"android:layout_height="wrap_content"机器人:重力=中心"机器人:layout_centerVertical =真"机器人:layout_centerInParent =真"android:textSize="17dp"/><图像视图机器人:layout_gravity =正确"android:layout_width="30dp"android:layout_height="wrap_content"android:id="@+id/item2"android:src="@drawable/bell"android:layout_alignParentRight="true"/></RelativeLayout>
输出截图如下:
如果你使用的是基于
此外,如果您需要应用一些依赖于平台的特殊调整,您可以使用 OnPlatform.
为什么我选择样式的那些属性?
尽可能接近原生默认文本标题.相关问题:
定义 Shell.TitleView,同时保留 shell 中标签的标题文本,但似乎以不同的字体显示>
I am adding custom view in Center of ToolbarItem from NavigationPageRenderer for android. But it doesn't seems to be working. See my code below
public class CustomNavigationPageRenderer : NavigationPageRenderer
{
public override void OnViewAdded(Android.Views.View child)
{
base.OnViewAdded(child);
var lastPageElement?.Navigation?.NavigationStack?.Last().ToString();
if (lastPage == "ParentDashboardPage")
{
LayoutInflater li = LayoutInflater.From(Context);
Android.Views.View customView = li.Inflate(Resource.Layout.customActionbar, null);
var _toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
_toolbar.RemoveAllViews();
//_toolbar.AddView(text,new Toolbar.LayoutParams(GravityFlags.Center)); Even this not working
_toolbar.AddView(customView);
}
}
}
customActionbar.axml file below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:text=" Dashboard"
android:textColor="@color/white"
android:layout_gravity="center"
android:id="@+id/tvActionBarTitle"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
android:textSize="17dp" />
<ImageView
android:layout_gravity="right"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:id="@+id/item2"
android:src="@drawable/bell"
android:layout_alignParentRight="true" />
</RelativeLayout>
Output screensshot below:
If you are using a Xamarin.Forms app based on Shell then you can easily achieve this using Shell.TitleView without the need of a custom renderer, define your title content, bind it or even style it as you like.
Inside your ContentPage
:
<Shell.TitleView>
<StackLayout Orientation="Horizontal" Spacing="0">
<Label Text="Dashboard"
Opacity="1"
FontAttributes="Bold"
FontSize="20"
TextColor="White"
HorizontalOptions="CenterAndExpand"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"/>
<ImageButton Source="Bell.png"
VerticalOptions="CenterAndExpand"
HorizontalOptions="End"
BackgroundColor="transparent"/>
</StackLayout>
</Shell.TitleView>
Also if you need to apply some special adjustments platform-dependent you can use OnPlatform.
Why I choosed those attributes of the style?
To be as close as possible to native default text title. Related question:
这篇关于如何从渲染器在 ToolbarItem 的中心添加自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!