将ActionBar标题与Xamarin中心对齐

时间:2015-12-10 17:53:32

标签: android android-activity xamarin android-actionbar xamarin.forms

我正在使用Xamarin Forms,我删除了我的图标,现在我想把Action Bar标题放在中心,实际上它在左边。

我看到一些页面说我使用$ cd /ns-allinone-2.35/ns-2.35/ $ make 但是在我的visual studio中找不到,我试图在Style.xml或MainActivity上删除它。

enter image description here

MainActivity.XML

getSupportActionBar()

Style.xml:

using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

namespace App5.Droid
{
    [Activity(Label = "App5", Icon = "@drawable/icon", Theme = "@style/CustomActionBarTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {        
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);            
            global::Xamarin.Forms.Forms.Init(this, bundle);
            ActionBar.SetIcon(Android.Resource.Color.Transparent);
            LoadApplication(new App());
        }

    }
}

1 个答案:

答案 0 :(得分:1)

使用Xamarin,唯一的方法是以编程方式:

LinearLayout lLayout = new LinearLayout(activity);
lLayout .SetGravity(GravityFlags.CenterVertical);
lLayout .LayoutParams textViewParameters = 
                        new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
textViewParameters.RightMargin = (int)(30 * activity.Resources.DisplayMetrics.Density);

TextView myTitle = new TextView(activity);        
myTitle.Text = text;
myTitle.Gravity = GravityFlags.Center;
lLayout.AddView(myTitle ,textViewParameters);

ActionBar.LayoutParams actionbarParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.MatchParent,ActionBar.LayoutParams.MatchParent);
activity.ActionBar.SetCustomView(lLayout, actionbarParams);