我做了很多研究:
- 如何更改 ActionBar Tab textStyle?
- 小写的 TabLayout 选项卡标题文本
- https://github.com/Andy671/Dachshund-Tab-Layout/issues/2
- https://github.com/NativeScript/NativeScript/issues/2551
但是,我找不到如何正确实现我的想法。
我的应用程序使用带有弹出菜单的 Xamarin Shell,并且某些页面显示了一个顶部选项卡栏。举个例子,我的应用程序如下所示:
Xamarin Shell 提供了一种创建这些多选项卡页面的简单方法。现在,我想自定义这些选项卡并更改字体、选择指示器的颜色等。一开始,我以为我可以在styles.xml
我的 Android 项目的文件中创建一个特定的样式,然后在android.support.design.widget.TabLayout
. 举个例子,我在Tabbar.xml
我的 Xamarin 解决方案的 Android 项目中的文件中做了这样的事情:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="@android:color/white"
style="@style/MyCustomTabLayout"
app:tabGravity="fill"
app:tabMode="fixed" />
这在styles.xml
文件中总是在解决方案的Android项目中:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MainTheme" parent="MainTheme.Base">
<item name="android:textAllCaps">false</item>
</style>
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#FFFFFF</item>
<item name="tabIndicatorHeight">3dp</item>
<item name="tabSelectedTextColor">#FFFFFF</item>
</style>
</resources>
但是,什么也没发生,样式也没有应用。我认为这是对如何真正实现我的想法的误解,并且我认为“子”顶部标签栏可能不被认为是真实的TabLayout
,因为它只是ShellSection
. 最后,我发现也许我需要的是一个自定义渲染器。我正在实施它,但我被困在这个问题上:我不明白如何设置ShellSection
. 我当前的自定义渲染器代码是这样的:
[assembly: ExportRenderer(typeof(AppShell), typeof(CustomShellRenderer))]
namespace A {
internal class CustomShellRenderer : ShellRenderer {
public CustomShellRenderer(Context context) : base(context) { }
protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection) {
return new CustomShellSectionAppearance(this);
}
}
class CustomShellSectionAppearance : ShellSectionRenderer {
public Fragment Fragment { get; }
public event EventHandler AnimationFinished;
public void Dispose() {
throw new NotImplementedException();
}
public ShellSection ShellSection { get; set; }
public CustomShellSectionAppearance(IShellContext shellContext) : base(shellContext) { }
// I thought I need to make my customization here, but it was only a guess:
// I found nobody talking about customizing a ShellSection on the Web
protected override void SetAppearance(ShellAppearance appearance) {
base.SetAppearance(appearance);
appearance.TabBarDisabledColor = Color.Aqua; // ERROR: appearance has only "get" properties
}
}
}
在自定义 Xamarin 应用程序顶部选项卡栏的外观的过程中,我是否遗漏了什么?