- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想放置 float 操作按钮。按钮必须是动态的(它将是动画的,并且底座必须像从 material.io 拍摄的视频中一样适应它)。它必须像 Android 一样与 iOS 一起使用。 XAML 示例(如果可以)。也许自定义渲染器。
我知道这个功能默认存在于 Android Studio 和 Flutter 中。
如果我的问题中有什么地方不对,请写信给我,对不起我的英语。
最佳答案
您将不得不亲自动手编写代码。我的解决方案适用于 Android,我也很想知道它在 iOS 中是如何完成的。我依赖 nuget 包 Xamarin.Forms.Visual.Material .可以找到一篇让我入门的好文章 here还有here .还可以查看 Google 的 Material 设计 here .
在您的 Android 项目中,创建一个包含以下内容的 BottomTabLayout.xml 文件:
<?xml version="1.0" encoding="utf-8" ?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- Note: A RecyclerView can also be used -->
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="100dp"
android:clipToPadding="false">
<!-- Scrollable content -->
<FrameLayout
android:id="@+id/bottomtab.navarea"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="fill"
android:layout_weight="1" />
</androidx.core.widget.NestedScrollView>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp"
app:fabCradleMargin="10dp">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomtab.tabbar"
android:theme="@style/Widget.Design.BottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginEnd="0dp"
android:background="@android:color/transparent"/>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fab"
app:layout_anchor="@id/bottomAppBar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
并且在 style.xml 文件中,如下修改其内容以使用任何 Material Themes:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MainTheme" parent="Theme.MaterialComponents.Light">
<!-- As of Xamarin.Forms 4.6 the theme has moved into the Forms binary -->
<!-- If you want to override anything you can do that here. -->
<!-- Underneath are a couple of entries to get you started. -->
<!-- Set theme colors from https://aka.ms/material-colors -->
<!-- colorPrimary is used for the default action bar background -->
<!--<item name="colorPrimary">#2196F3</item>-->
<!-- colorPrimaryDark is used for the status bar -->
<!--<item name="colorPrimaryDark">#1976D2</item>-->
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<!--<item name="colorAccent">#FF4081</item>-->
<item name="android:windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
创建一个名为“Renderers”的文件夹并添加以下类:
BottomNavigationViewUtils2.cs(处理动态菜单生成)
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using FabTabBarDemo.Droid.Extensions;
using Google.Android.Material.BottomNavigation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
namespace FabTabBarDemo.Droid.Renderers
{
internal class BottomNavigationViewUtils2
{
internal const int MoreTabId = 99;
internal static void UpdateEnabled(bool tabEnabled, IMenuItem menuItem)
{
if (menuItem.IsEnabled != tabEnabled)
menuItem.SetEnabled(tabEnabled);
}
internal static async void SetupMenu(IMenu menu, int maxBottomItems,
List<(string title, ImageSource icon, bool tabEnabled)> items,
int currentIndex, BottomNavigationView bottomView, Context context, bool hasFab = false)
{
menu.Clear();
int numberOfMenuItems = items.Count;
bool showMore = numberOfMenuItems > maxBottomItems;
int end = showMore ? maxBottomItems - 1 : numberOfMenuItems;
List<IMenuItem> menuItems = new List<IMenuItem>();
List<Task> loadTasks = new List<Task>();
for (int i = 0; i < end; i++)
{
var item = items[i];
using var title = new Java.Lang.String(item.title);
var menuItem = menu.Add(0, i, 0, title);
menuItems.Add(menuItem);
loadTasks.Add(SetMenuItemIcon(menuItem, item.icon, context));
UpdateEnabled(item.tabEnabled, menuItem);
if (i == currentIndex)
{
menuItem.SetChecked(true);
bottomView.SelectedItemId = i;
}
if (hasFab)
{
int redundantMenus = CanAddRedundantMenu(numberOfMenuItems, i);
for (int x = 0; x < redundantMenus; x++)
{
menuItem = menu.Add(0, x + 20, 0, "");
UpdateEnabled(false, menuItem);
}
}
}
if (showMore)
{
var moreString = context.Resources.GetText(Resource.String.overflow_tab_title);
var menuItem = menu.Add(0, MoreTabId, 0, moreString);
menuItems.Add(menuItem);
menuItem.SetIcon(Resource.Drawable.abc_ic_menu_overflow_material);
if (currentIndex >= maxBottomItems - 1)
menuItem.SetChecked(true);
}
bottomView.SetShiftMode(false, false);
if (loadTasks.Count > 0)
await Task.WhenAll(loadTasks);
foreach (var menuItem in menuItems)
menuItem.Dispose();
}
/// <summary>
/// Gets a value that determines whether a redundant menu can be added to
/// the <see cref="BottomNavigationView"/>.
/// </summary>
/// <param name="menuCount">The total menu entries in the view.</param>
/// <param name="currentIndex">The zero-based menu index.</param>
/// <returns>The total number of redundant menus to add or -1.</returns>
internal static int CanAddRedundantMenu(int menuCount, int currentIndex)
{
return menuCount switch
{
1 => currentIndex == 0 ? 2 : -1,
3 => currentIndex == 1 ? 2 : -1,
2 => currentIndex == 0 ? 1 : -1,
4 => currentIndex == 1 ? 1 : -1,
_ => -1,
};
}
static async Task SetMenuItemIcon(IMenuItem menuItem, ImageSource source, Context context)
{
if (source == null)
return;
var drawable = await source.GetDrawableAsync(context);
//var drawable = await context.GetFormsDrawableAsync(source);
menuItem.SetIcon(drawable);
drawable?.Dispose();
}
}
}
FabBottomNavViewAppearanceTracker.cs(管理 BottomNavigationView 渲染)
using Google.Android.Material.BottomNavigation;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
namespace FabTabBarDemo.Droid.Renderers
{
public class FabBottomNavViewAppearanceTracker : ShellBottomNavViewAppearanceTracker
{
public FabBottomNavViewAppearanceTracker(IShellContext shellContext, ShellItem shellItem) : base(shellContext, shellItem)
{
}
protected override void SetBackgroundColor(BottomNavigationView bottomView, Color color)
{
// Prevent the default xamarin forms background rendering of BottomNavigationView
}
}
}
FabShellItemRenderer.cs(管理 shell 选项卡和处理渲染)
using Android.Content.Res;
using Android.OS;
using Android.Views;
using Android.Widget;
using Google.Android.Material.BottomAppBar;
using Google.Android.Material.BottomNavigation;
using Google.Android.Material.FloatingActionButton;
using System.Collections.Generic;
using System.Reflection;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
namespace FabTabBarDemo.Droid.Renderers
{
public class FabShellItemRenderer : ShellItemRenderer
{
private readonly FieldInfo navigationAreaField;
private readonly FieldInfo bottomViewField;
BottomAppBar _bottomAppBar;
FloatingActionButton _fab;
BottomNavigationView _bottomView;
IShellBottomNavViewAppearanceTracker appearanceTracker;
public FabShellItemRenderer(IShellContext shellContext) : base(shellContext)
{
var baseType = typeof(ShellItemRenderer);
navigationAreaField = baseType.GetField("_navigationArea", BindingFlags.Instance | BindingFlags.NonPublic);
bottomViewField = baseType.GetField("_bottomView", BindingFlags.Instance | BindingFlags.NonPublic);
}
protected BottomNavigationView BottomView => (BottomNavigationView)bottomViewField.GetValue(this);
public override Android.Views.View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var layout = inflater.Inflate(Resource.Layout.BottomTabLayout, null);
var _navigationArea = layout.FindViewById<FrameLayout>(Resource.Id.bottomtab_navarea);
_bottomAppBar = layout.FindViewById<BottomAppBar>(Resource.Id.bottomAppBar);
_fab = layout.FindViewById<FloatingActionButton>(Resource.Id.fab);
_bottomView = layout.FindViewById<BottomNavigationView>(Resource.Id.bottomtab_tabbar);
_bottomView.Background = null;
_bottomView.SetOnNavigationItemSelectedListener(this);
navigationAreaField.SetValue(this, _navigationArea);
bottomViewField.SetValue(this, _bottomView);
HookEvents(ShellItem);
SetupMenu();
appearanceTracker = ShellContext.CreateBottomNavViewAppearanceTracker(ShellItem);
((IShellController)ShellContext.Shell).AddAppearanceObserver(this, ShellItem);
return layout;
}
void SetupMenu()
{
using var menu = _bottomView.Menu;
SetupMenu(menu, _bottomView.MaxItemCount, ShellItem);
}
protected override void ResetAppearance() => appearanceTracker?.ResetAppearance(BottomView);
protected override void SetAppearance(ShellAppearance appearance)
{
IShellAppearanceElement controller = appearance;
var backgroundColor = controller.EffectiveTabBarBackgroundColor.ToAndroid();
_bottomAppBar.BackgroundTintList = ColorStateList.ValueOf(backgroundColor);
_fab.BackgroundTintList = ColorStateList.ValueOf(backgroundColor);
appearanceTracker?.SetAppearance(BottomView, appearance);
}
protected override void SetupMenu(IMenu menu, int maxBottomItems, ShellItem shellItem)
{
var _bottomView = BottomView;
_bottomView.SetBackgroundColor(Android.Graphics.Color.Transparent);
_bottomView.Background = null;
var currentIndex = ((IShellItemController)ShellItem).GetItems().IndexOf(ShellSection);
var items = CreateTabList(shellItem);
BottomNavigationViewUtils2.SetupMenu(menu, maxBottomItems, items, currentIndex, _bottomView, Context, true);
UpdateTabBarVisibility();
}
List<(string title, ImageSource icon, bool tabEnabled)> CreateTabList(ShellItem shellItem)
{
var items = new List<(string title, ImageSource icon, bool tabEnabled)>();
var shellItems = ((IShellItemController)shellItem).GetItems();
for (int i = 0; i < shellItems.Count; i++)
{
var item = shellItems[i];
items.Add((item.Title, item.Icon, item.IsEnabled));
}
return items;
}
}
}
FabShellRenderer.cs(项目的 Shell 渲染器)
using Android.Content;
using FabTabBarDemo.Droid.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(Shell), typeof(FabShellRenderer))]
namespace FabTabBarDemo.Droid.Renderers
{
public class FabShellRenderer : ShellRenderer
{
public FabShellRenderer(Context context) : base(context)
{
}
protected override IShellItemRenderer CreateShellItemRenderer(ShellItem shellItem)
{
return new FabShellItemRenderer(this);
}
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new FabBottomNavViewAppearanceTracker(this, shellItem);
}
}
}
最后……ImageSourceExtensions.cs(一些基本的ImageSource逻辑)
using Android.Content;
using Android.Graphics.Drawables;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
namespace FabTabBarDemo.Droid.Extensions
{
internal static class ImageSourceExtensions
{
public static IImageSourceHandler GetHandler(this ImageSource imageSource)
{
if (imageSource is UriImageSource)
return new ImageLoaderSourceHandler();
else if (imageSource is FileImageSource)
return new FileImageSourceHandler();
else if (imageSource is StreamImageSource)
return new StreamImagesourceHandler();
else if (imageSource is FontImageSource)
return new FontImageSourceHandler();
return null;
}
public static async Task<Drawable> GetDrawableAsync(this ImageSource imageSource, Context context)
{
var imageHandler = imageSource.GetHandler();
if (imageHandler == null)
return null;
var bitmap = await imageHandler.LoadImageAsync(imageSource, context);
return new BitmapDrawable(context.Resources, bitmap);
}
}
}
在您的 Xamarin 共享项目(.Net Standard with Shell)中,根据需要使用 TabBar。下面是一个例子:
<TabBar>
<ShellContent Title="Home" Route="HomePage" ContentTemplate="{DataTemplate local:HomePage}">
<ShellContent.Icon>
<FontImageSource FontFamily="{StaticResource FontAwesomeSolid}"
Glyph="{x:Static fonts:FaSolidIcons.Home}" />
</ShellContent.Icon>
</ShellContent>
<ShellContent Title="Search" Route="SearchPage" ContentTemplate="{DataTemplate local:SearchPage}">
<ShellContent.Icon>
<FontImageSource FontFamily="{StaticResource FontAwesomeSolid}"
Glyph="{x:Static fonts:FaSolidIcons.Search}" />
</ShellContent.Icon>
</ShellContent>
<ShellContent Title="Profile" Route="ProfilePage" ContentTemplate="{DataTemplate local:ProfilePage}">
<ShellContent.Icon>
<FontImageSource FontFamily="{StaticResource FontAwesomeSolid}"
Glyph="{x:Static fonts:FaSolidIcons.User}" />
</ShellContent.Icon>
</ShellContent>
<ShellContent Title="Settings" Route="SettingsPage" ContentTemplate="{DataTemplate local:SettingsPage}">
<ShellContent.Icon>
<FontImageSource FontFamily="{StaticResource FontAwesomeSolid}"
Glyph="{x:Static fonts:FaSolidIcons.Cog}" />
</ShellContent.Icon>
</ShellContent>
</TabBar>
就是这样。结果如下图:
注意:FAB 的主题和图标很容易完成,这取决于任何感兴趣的人。编码愉快!
关于xaml - Xamarin.Forms - Gradle FAB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64887786/
我正在处理一些数字代码,我正在查看编译器输出。一个特殊的案例让我觉得很奇怪: 在实数中,它成立 abs(a) * abs(b) = abs(a * b) .我希望在浮点数中也是如此。但是,优化既不是由
是否有人已经实现了将扩展 FAB 转换为标准 FAB,反之亦然?我知道如何将 FAB 添加到我的布局中以及如何检测列表滚动阈值,但我真的不知道如何实现这种转换(请参阅引用资料以了解我想做什么:Exte
我正在为我的 ionic 应用程序使用 Angular Material 。我发现了 fab 工具栏,并将其包含在我的应用程序中。这是 Angular Material 指南中提供的演示。 FAB t
我正在尝试使用 Materialize 在垂直 FAB 内创建水平 FAB。下面的代码几乎可以完美运行,但是每当我将鼠标悬停在主 FAB 上时,它都会切换主 FAB 和第二个 FAB,但我只想在将鼠标
我对 abs() 和 fabs() 函数进行了一些简单的测试,但我不明白使用 fabs() 有什么好处,如果是的话: 1) 慢 2) 仅适用于 float 3) 如果用于不同的类型会抛出异常 In [
背景 当你接到电话时,我正在制作一个上下滑动 FAB 的 POC,类似于它在电话应用程序上的工作方式: 问题 虽然我已经处理了触摸事件(允许上下移动 fab),并在您停止触摸时返回动画,但我还需要更改
我正试图在GNU C中创建一个返回32位浮点数绝对值的FABS函数。我有三种不同的方法,称为fab1、fab2和fab3:。有三个不同的函数,一个使用简单的判定,一个使用联合,最后一个使用x86汇编。
我无法以编程方式隐藏弧形菜单中的 fab 按钮。我正在使用https://github.com/saurabharora90/MaterialArcMenu在我的代码中。如何在Java中以编程方式隐藏
我正在尝试使用 Clans/FloatingActionButton 创建一个 float 操作菜单但我在布局文件中收到命名空间“fab”未绑定(bind)错误。我已经包含了 compile 'com
我在布局文件中将我的 FAB 定义为: 这里@drawable/search是我从 Android Studio 的 Vector Asset 工具(Asset Type Material Icon
今天,我正在进行一个登录 Activity ,该 Activity 使用FAB作为登录按钮。密码是正确的。当我布置布局文件时,我使用了以下代码: android:id="@+id/floatingAc
Fab-toolbar 在主演示 here 提供的示例中看起来不错。 但是,如果您尝试在自己的示例中使用它,它会变得比应有的高度高,如 here 所示。 . You ca
根据更新的 Material 指南, float 操作按钮可以转换为菜单,如下所示: 有人可以帮助我提供一些代码或一些有关如何实现此行为的链接吗? 谢谢你。 最佳答案 试试这个方法 在下方添加depe
为什么这个简单的代码不起作用?最初,我使用 xml 代码 app:fabSize="mini" 将 fab 图标的大小设置为 mini 然后我用java代码中的这个小 fragment 来互换大小
我在 appStore 上有较旧的应用程序。我想为 iOS 7 更新它,所以我在 Xcode 5 下打开它,并在 Build Settings 下打开了 Objective-c 模块。没有它,编译会很
我需要创建类似的东西。这可能是重复的,我不确定如何搜索它。如果你按加号,它会扩展更多的按钮 最好的问候 最佳答案 不再需要创建您自己的 FAB 或使用第三方库,它已包含在 AppCompat 22 中
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
几天前我拥有完美的 FAB,现在图像无法居中,这让我抓狂!这是我现在得到的: 我正在使用 CardView 中的按钮。这是按钮 xml 标记: 这是可绘制文件:ic_options.xml 奇
我有一个 float 操作按钮菜单,它在点击时展开菜单项。如何实现与下图所示相同的行为?请帮忙。 我目前有以下布局语法:
我正在使用 FloatingActionButton 图标来共享资源我的图标看起来像这样 看起来灰色是重叠。 FloatingActionButton 图标的 xml 是 但是我希望白色像这个图标一
我是一名优秀的程序员,十分优秀!