Java 如何使用真正的浮动操作按钮 (FAB) 扩展?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50354930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How use the real Floating Action Button (FAB) Extended?
提问by Canato
To remove any doubts or thoughts about duplicate: on Material Designis defined what is "extended".
消除对重复的任何疑问或想法:在Material Design上定义了什么是“扩展”。
But most of the people confuses "extended" with "Type of Transition: Speed Dial", what make hard to find solutions. Like here
但大多数人将“扩展”与“转换类型:快速拨号”混淆,很难找到解决方案。喜欢这里
QuestionSo what I'm looking forward is how setup the FAB with text and a extended size.
问题所以我期待的是如何设置带有文本和扩展尺寸的 FAB。
Today my code is like this:
今天我的代码是这样的:
<android.support.design.widget.FloatingActionButton
android:id="@+id/maps_main_distance_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="@string/str_distance_btn"
app:elevation="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
But my button look like this:
但我的按钮看起来像这样:
No text and no right format. I'm using it in a Constraint Layout.
没有文字,也没有正确的格式。我在约束布局中使用它。
采纳答案by Alexander HD
- Add the dependencies in your Gradle file:
- 在 Gradle 文件中添加依赖项:
implementation 'com.google.android.material:material:1.1.0-alpha04'
in your xml file:
在您的 xml 文件中:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="@dimen/fab_margin"
android:text="Create"
app:icon="@drawable/outline_home_24" />
回答by Arthulia
The short answer: set the background to a rect with rounded corners, rather than an oval. Then set the dimension of the radius to half that of the height of the button.
简短的回答:将背景设置为带有圆角的矩形,而不是椭圆形。然后将半径的尺寸设置为按钮高度的一半。
How I actually did it:
我实际上是如何做到的:
<android.support.design.widget.FloatingActionButton
android:id="@+id/maps_main_distance_btn"
android:layout_width="@dimen/floating_button_width"
android:layout_height="@dimen/floating_button_height"
android:layout_marginBottom="8dp"
android:background="@drawable/btn_background_expanded"
android:text="@string/str_distance_btn"
app:elevation="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
drawable/btn_background_expanded.xml:
drawable/btn_background_expanded.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item>
<shape android:shape="rectangle">
<solid android:color="?android:colorAccent" />
<corners
android:radius="@dimen/floating_button_radius"
/>
</shape>
</item>
</ripple>
回答by Mahdi Moqadasi
You can use this libraryto do that. (I used another way) But I used a FancyButtonto create a round and awesome button in coordinator layout as below:
你可以使用这个库来做到这一点。(我使用了另一种方式)但是我使用了FancyButton在协调器布局中创建了一个圆形且很棒的按钮,如下所示:
<mehdi.sakout.fancybuttons.FancyButton
android:id="@+id/actMain_btnCompare"
app:layout_behavior="@string/fab_transformation_sheet_behavior"
android:layout_width="125dp"
android:layout_height="38dp"
android:layout_gravity="bottom|center"
android:layout_margin="20dp"
android:elevation="3dp"
android:padding="2dp"
fancy:fb_borderColor="@color/white"
fancy:fb_borderWidth="3dp"
fancy:fb_defaultColor="@color/colorPrimaryDark"
fancy:fb_radius="20dp"
fancy:fb_text="?????? ?????"
fancy:fb_textColor="@color/white"
fancy:fb_textGravity="center"
fancy:fb_textSize="13sp" />
and result is:
结果是:
That can have icon in it (see FancyButton).
里面可以有图标(见FancyButton)。
with app:layout_behavior="@string/fab_transformation_sheet_behavior"
it acts like fab as below:
与app:layout_behavior="@string/fab_transformation_sheet_behavior"
它的作用就像如下FAB:
Good luck.
祝你好运。
回答by Tunji_D
You can create a utility class that animates a MaterialButton
to an Extended FloatingActionButton
using a ConstraintLayout
. You'll first need to declare the two states of the MaterialButton
in xml, and then use the TransitionManager
to animate between them.
您可以创建一个动画一个实用工具类MaterialButton
,以一个扩展FloatingActionButton
使用ConstraintLayout
。您首先需要MaterialButton
在 xml 中声明 的两个状态,然后使用TransitionManager
来在它们之间设置动画。
You can read a medium post about it here, in the meantime, I'll add bits of relevant code here.
你可以在这里阅读一篇关于它的中等文章,同时,我会在这里添加一些相关的代码。
Collapsed FAB state:
折叠的 FAB 状态:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout android:id="@+id/extend_fab_container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_fab"
android:elevation="8dp">
<android.support.design.button.MaterialButton
android:id="@+id/fab"
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
android:layout_width="56dp"
android:layout_height="56dp"
app:cornerRadius="56dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Extended FAB State:
扩展 FAB 状态:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout android:id="@+id/extend_fab_container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="8dp"
android:background="@drawable/bg_fab">
<android.support.design.button.MaterialButton
android:id="@+id/fab"
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cornerRadius="56dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Background Drawable:
背景可绘制:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorAccent" />
<corners android:radius="56dp" />
</shape>
And relevant Java Expansion code:
以及相关的 Java 扩展代码:
private void setExtended(boolean extended, boolean force) {
if (isAnimating || (extended && isExtended() && !force)) return;
ConstraintSet set = new ConstraintSet();
set.clone(container.getContext(), extended ? R.layout.fab_extended : R.layout.fab_collapsed);
TransitionManager.beginDelayedTransition(container, new AutoTransition()
.addListener(listener).setDuration(150));
if (extended) button.setText(currentText);
else button.setText("");
set.applyTo(container);
}