【发布时间】:2021-03-20 00:17:12
【问题描述】:
我做了很多研究:
- How to change ActionBar Tab textStyle?
- TabLayout Tab Title text in Lower Case
- https://github.com/Andy671/Dachshund-Tab-Layout/issues/2
- https://github.com/NativeScript/NativeScript/issues/2551
但是,我找不到如何正确实现我的想法。
我的应用程序使用带有弹出菜单的 Xamarin Shell,并且某些页面显示了顶部选项卡栏。举个例子,我的应用是这样的:
Xamarin Shell provides a simple way to create these multi-tabs pages。现在,我想自定义这些选项卡并更改字体、选择指示器的颜色等。一开始,我以为我可以在我的Android项目的styles.xml文件中创建一个特定的样式,然后在android.support.design.widget.TabLayout中引用它。举个例子,我在 Xamarin 解决方案的 Android 项目中的 Tabbar.xml 文件中做了这样的事情:
<?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 的产物。最后,我发现也许我需要的是custom renderer。我正在实现它,但我被困在这个问题上:我不明白如何设置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 应用程序顶部选项卡栏的外观的过程中是否遗漏了什么?
【问题讨论】:
标签: c# android xamarin.forms custom-renderer xamarin.forms.shell