我正在使用氏族 fab 库来创建一个带有一组按钮的菜单。
我的菜单工厂作为一个开放方向设置为“向上”,如果手机处于纵向模式,这工作得很好。但是,如果我将其转为横向模式,则一半的按钮会被裁剪。
我想知道是否可以在我的 onConfigurationChanged 方法上以编程方式更改 fab 菜单的打开方向。
像这样的东西:
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// set menu fab orientation to start|left
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
// set menu fab orientation to up
}
}
我似乎无法找到以编程方式更改此设置的方法。有人知道怎么做吗?
不幸的是,浮动操作菜单目前仅支持向上和向下打开方向。
向上和向下扩展菜单的选项
目前也没有以编程方式更改配置方向的选项:它是在 XML 中设置的。
添加水平布局按钮的选项需要对库进行重大更改,因为未设置测量和布局例程来处理这种情况。
鉴于当菜单水平显示时标签会从上到下排列,您的用户体验无论如何都会受到负面影响。此外,大量的菜单项在某种程度上违背了 FAB的设计意图。
浮动操作按钮代表应用程序中的主要操作。浮动操作按钮用于提升的操作。
鉴于所有这些,我强烈建议您重新考虑您的方法,并限制您添加到 FAB 菜单的项目数量,以便项目适合两个方向。将不适合选项菜单或位置良好的上下文弹出菜单的项目移动。
作为旁注,氏族的回购是生命的终结。我不知道谁会接手它,但它目前的状态是有风险的。
一种情况是,repo 不允许基于配置进行编程更改,但有一种解决方法:使用基于配置的片段来完成相同的结果。例如:
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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="com.example.fabmenu.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"/>
<!-- Replace the FAB with a fragment reference -->
<include layout="@layout/fragment_fab_menu"/>
</android.support.design.widget.CoordinatorLayout>
现在我们创建两个文件,一个用于纵向,一个用于横向:
res/layout/fragment_fab_portrait.xml
<?xml version="1.0" encoding="utf-8"?>
<com.github.clans.fab.FloatingActionMenu
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"
...
app:menu_openDirection="left"
>
<com.github.clans.fab.FloatingActionButton
android:id="@+id/menu_item_horse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/fab_add"
app:fab_colorNormal="@color/colorAccent"
app:fab_label="Horse"
app:fab_size="mini"/>
...
</com.github.clans.fab.FloatingActionMenu>
res/layout/fragment_fab_landscape.xml(注意:'left'
在这种情况下是虚构的。)
<?xml version="1.0" encoding="utf-8"?>
<com.github.clans.fab.FloatingActionMenu
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"
...
app:menu_openDirection="left"
>
<com.github.clans.fab.FloatingActionButton
android:id="@+id/menu_item_horse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/fab_add"
app:fab_colorNormal="@color/colorAccent"
app:fab_label="Horse"
app:fab_size="mini"/>
...
</com.github.clans.fab.FloatingActionMenu>
这两个文件之间的唯一区别是我们需要根据配置进行布局更改。
现在我们添加魔法将它们整合在一起。两个文件让 Android 知道选择哪个文件。
/res/values/layouts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="fragment_fab_menu" type="layout">@layout/fragment_fab_portrait</item>
</resources>
/res/values/layouts-land.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Here name refers to the id we gave it in activity_main.xml -->
<item name="fragment_fab_menu" type="layout">@layout/fragment_fab_landscape</item>
</resources>
有关更多信息,请参阅支持不同的屏幕尺寸
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句