gpt4 book ai didi

android - 将 ActionBar 标题对齐 Xamarin

转载 作者:行者123 更新时间:2023-11-30 01:42:50 24 4
gpt4 key购买 nike

我正在使用 Xamarin Forms,我删除了我的图标,现在我想将操作栏标题放在中间,实际上它在左边。

我看到一些页面说我使用 getSupportActionBar() 但在我的 visual studio 上找不到它,我试图在我的 Style.xml 或我的 MainActivity 上删除它。

enter image description here

MainActivity.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());
}

}
}

样式.xml:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>

</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#000000</item>

</style>

</resources>

最佳答案

使用 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);

关于android - 将 ActionBar 标题对齐 Xamarin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34208592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com