我正在启动我的第一个 Xamarin Forms 应用程序 - 目前只是 Hello World - 但是我需要能够将标题栏中的文本居中。我正在使用一个 MasterDetailPage,它有一个 ContentPage 作为 Master,一个 NavigationPage 作为 Detail。从下面的屏幕截图中,我希望能够将“主页”居中。
我已经尝试过标记,例如在 AppCompact 主题中更改操作栏选项卡文本大小的答案,但是似乎没有任何影响文本的位置。我还尝试了自定义渲染器,但是没有用。
有没有人能够做到这一点?
用一些代码更新我的问题。我的 MasterDeutil.cs:
public class MasterDetail : MasterDetailPage
{
Master master;
public MasterDetail()
{
master = new Master();
Master = master;
Detail = new NavigationPage(new Home());
master.ListView.ItemSelected += ListView_ItemSelected;
ToolbarItem cart = new ToolbarItem()
{
Text = "Test",
Icon = "shoppingcart.png"
};
cart.Clicked += async (object sender, System.EventArgs e) =>
{
await DisplayAlert("Done", "Msg", "Ok", "Can");
};
ToolbarItems.Add(cart);
}
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
MasterItem item = e.SelectedItem as MasterItem;
if (item != null)
{
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
master.ListView.SelectedItem = null;
IsPresented = false;
}
}
}
Master.cs 的代码:
public class MasterDetail : MasterDetailPage
{
Master master;
public MasterDetail()
{
master = new Master();
Master = master;
Detail = new NavigationPage(new Home());
master.ListView.ItemSelected += ListView_ItemSelected;
ToolbarItem cart = new ToolbarItem()
{
Text = "Test",
Icon = "shoppingcart.png"
};
cart.Clicked += async (object sender, System.EventArgs e) =>
{
await DisplayAlert("Done", "Msg", "Ok", "Can");
};
ToolbarItems.Add(cart);
}
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
MasterItem item = e.SelectedItem as MasterItem;
if (item != null)
{
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
master.ListView.SelectedItem = null;
IsPresented = false;
}
}
}
Detail.cs 的代码:
public class Detail : ContentPage
{
public Detail()
{
Title = "Shaves2U";
Content = new StackLayout
{
Children = {
new Label {
Text = "Detail data goes here",
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.CenterAndExpand
}
}
};
}
}
样式.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!-- Base theme applied no matter what API -->
<style name="MainTheme" parent="Theme.AppCompat">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
<!-- colorPrimary is used for the default action bar background -->
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
</resources>
我尝试了以下 style.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!-- Base theme applied no matter what API -->
<style name="MainTheme" parent="Theme.AppCompat">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
<!-- colorPrimary is used for the default action bar background -->
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
<item name="android:actionBarTabTextStyle">@style/CustomTab</item>
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
<style name="CustomTab" parent="@android:style/Widget.AppCompat.ActionBar.TabText">
<item name="android:gravity">center</item>
</style>
</resources>
我希望我刚刚弄错了 XML