I'm starting my first Xamarin Forms application - little more than Hello World at the moment - however I need to be able to Center the text in the title bar. I'm using a MasterDetailPage which has a ContentPage as Master and a NavigationPage as Detail. From the screen shot below I'd like to be able to center 'Home'.
I've tried markup like the answer from Changing Actionbar Tab Text Size in AppCompact Theme however nothing seems to affect the position of the text. I also tried a custom renderer, however that didn't work.
Has anyone been able to do this?
Updating my question with some code. My MasterDeatil.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;
}
}
}
Code for 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;
}
}
}
Code for 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
}
}
};
}
}
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>
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
</resources>
I have tried the following 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>
I'm hoping I've just got the XML wrong
MasterDetailPage
? With just a screenshot attached to your question, it is difficult to know how we are to assist you. Also, please do note thatXamarin.Forms
uses the default behaviour of the respective platforms. When it comes to the title of a master-detail page for Android, the title is normally always left aligned. – DemitrianToolBar
's context, I used MainActivity as its context, but seems to be a wrong one. – Grace Feng