Java Android L - 浮动操作按钮 (FAB)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24451026/
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
Android L - Floating Action Button (FAB)
提问by yannickpulver
Did Google already released a defined style or a component for this new circular FAB button or should I implement the design on my own?
谷歌是否已经为这个新的圆形 FAB 按钮发布了定义的样式或组件,还是我应该自己实现设计?
The button is described here: Google Design | Floating Action Buttons
该按钮在此处描述:Google Design | 浮动操作按钮
EDIT (05/2015): Check Lukas' answer/ Gabriele's answershowing an easy way to implement it with the design support library.
编辑(05/2015):检查Lukas 的回答/ Gabriele 的回答,展示了一种使用设计支持库实现它的简单方法。
采纳答案by Daniele Segato
UPDATE: there's now an official widget for FAB: FloatingActionButton, see Gabriele Mariotti reply for full information.
更新:现在有一个 FAB 的官方小部件:FloatingActionButton,请参阅 Gabriele Mariotti 回复以获取完整信息。
According to Adam Powell and Chet Haase they didn't create a widget for the FAB button cause it's a very easy component to reproduce.
根据 Adam Powell 和 Chet Haase 的说法,他们没有为 FAB 按钮创建小部件,因为它是一个非常容易复制的组件。
There was a question in the Google IO 2014 speech "Google I/O 2014 - Material science: Developing Android applications with material design", at the end of the speech (at about 37:50) there was exactly that question, you can hear it here: https://www.youtube.com/watch?v=lSH9aKXjgt8#t=2280
Google IO 2014 演讲“Google I/O 2014 - Material science: Development Android applications with material design”中有一个问题,在演讲的最后(大约 37:50)就有了那个问题,你可以听到在这里:https: //www.youtube.com/watch?v=lSH9aKXjgt8#t=2280
Chet Haase says there's a RoundedBitmapDrawable (I didn't check if that's the name) that should already do the job of defining the Outline.
Chet Haase 说有一个 RoundedBitmapDrawable(我没有检查它是否是名字)应该已经完成了定义大纲的工作。
But you can do it with your own drawable, set an Elevation to it and define an circle Outline programmatically.
但是你可以用你自己的 drawable 来做,为它设置一个 Elevation 并以编程方式定义一个圆轮廓。
This should give you the round button with shadow on L release. But I think You'll have to build the Shadow pre-L on your own.
这应该为您提供带有 L 版本阴影的圆形按钮。但我认为你必须自己构建 Shadow pre-L。
I should check the code for CardView to see how it reproduce the shadow pre-L. I'll probably do that, but do not have time now. If no one pops in with the details I'll do it after I've found the time to go and check it up.
我应该检查 CardView 的代码,看看它如何重现阴影 pre-L。我可能会这样做,但现在没有时间。如果没有人提供详细信息,我会在找到时间去检查之后再做。
EDIT:
编辑:
Gabriele Mariotti (see his answer below, thank you) added some code to show you how to do it.
Gabriele Mariotti(请参阅下面的回答,谢谢)添加了一些代码来向您展示如何操作。
Thanks to @shomeser comments, he wrote a library to make the fab button:
感谢@shomeser 的评论,他写了一个库来制作 fab 按钮:
https://github.com/shamanland/floating-action-button
https://github.com/shamanland/floating-action-button
To use it:
要使用它:
dependencies {
compile 'com.shamanland:fab:0.0.3'
}
You can also read his answer to another question: How can I add the new "Floating Action Button" between two widgets/layouts
您还可以阅读他对另一个问题的回答:如何在两个小部件/布局之间添加新的“浮动操作按钮”
回答by Gabriele Mariotti
UPDATED: 16/08/2019 with the official Material components for android library
更新:16/08/2019 与 android 库的官方材料组件
With the new Material components for androidadd to your build.gradle
:
使用适用于 android的新材料组件添加到您的build.gradle
:
implementation 'com.google.android.material:material:1.0.0'
Then add in your layout:
然后在你的布局中添加:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floating_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
app:srcCompat="@drawable/ic_plus_24"/>
And use it:
并使用它:
FloatingActionButton floatingActionButton =
(FloatingActionButton) findViewById(R.id.floating_action_button);
floatingActionButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// Handle the click.
}
});
If you are using a Material Theme like Theme.MaterialComponents
your FAB will inherit the material style. Otherwise just apply the style @style/Widget.MaterialComponents.FloatingActionButton
如果您使用像Theme.MaterialComponents
FAB这样的 Material Theme将继承 Material 样式。否则只需应用样式@style/Widget.MaterialComponents.FloatingActionButton
<com.google.android.material.floatingactionbutton.FloatingActionButton
....
style="@style/Widget.MaterialComponents.FloatingActionButton"
..../>
More info here.
更多信息在这里。
UPDATED: 30/05/2015 with the official Design Support Library
更新:30/05/2015 官方设计支持库
There an official widget now.
现在有一个官方小部件。
Just add this dependency to your build.gradle
只需将此依赖项添加到您的 build.gradle
compile 'com.android.support:design:22.2.0'
Add this view to your layout:
将此视图添加到您的布局:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="@drawable/ic_done" />
And use it:
并使用它:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//TODO
}
});
Documentation:
文档:
- 安卓文档。
UPDATED: 02/12/2014 with Android 5 code
更新:02/12/2014 使用 Android 5 代码
Also you can add and stateListAnimatorto your Button:
您也可以将stateListAnimator添加到您的按钮:
<Button
android:stateListAnimator="@anim/anim"
/>
Where anim.xml is:
哪里 anim.xml 是:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="@dimen/button_elevation"
android:valueTo="@dimen/button_press_elevation"
android:valueType="floatType" />
</item>
<item>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="@dimen/button_press_elevation"
android:valueTo="@dimen/button_elevation"
android:valueType="floatType" />
</item>
</selector>
Dimens.xml is
Dimens.xml 是
<resources>
<dimen name="fab_size">56dp</dimen>
<dimen name="button_elevation">2dp</dimen>
<dimen name="button_press_elevation">4dp</dimen>
</resources>
Check the Daniele's answer.
检查丹尼尔的回答。
About Outline mentioned by Daniele. Add the elevationattribute to your Button, and set the Outlinevia code:
关于 Daniele 提到的 Outline。将海拔属性添加到您的按钮,并通过代码设置大纲:
<ImageButton
android:background="@drawable/ripple"
android:stateListAnimator="@anim/anim"
android:src="@drawable/ic_action_add"
android:elevation="4dp"
/>
About Outline:
关于大纲:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutfab);
//Outline: OLD METHOD IN L-PREVIEW
//int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
//Outline outline = new Outline();
//outline.setOval(0, 0, size, size);
//findViewById(R.id.fab).setOutline(outline);
Button fab = (Button) findViewById(R.id.fab);
ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
// Or read size directly from the view's width/height
int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
outline.setOval(0, 0, size, size);
}
};
fab.setOutlineProvider(viewOutlineProvider);
}
}
回答by Roman
Since Labels FAB feature (like in Evernote or Inbox apps) was added to this awesome libraryfeel free to use it:
由于标签 FAB 功能(例如在 Evernote 或 Inbox 应用程序中)已添加到这个很棒的库中,请随意使用它:
Gradle dependency:
Gradle依赖:
compile 'com.getbase:floatingactionbutton:1.3.0'
Layout.xml:
布局.xml:
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="@+id/multiple_actions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
fab:fab_addButtonColorNormal="@color/white"
fab:fab_addButtonColorPressed="@color/white_pressed"
fab:fab_addButtonPlusIconColor="@color/half_black"
fab:fab_labelStyle="@style/menu_labels_style"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp">
<com.getbase.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/white"
fab:fab_title="Action A"
fab:fab_colorPressed="@color/white_pressed"/>
<com.getbase.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/white"
fab:fab_title="Action B"
fab:fab_colorPressed="@color/white_pressed"/>
</com.getbase.floatingactionbutton.FloatingActionsMenu>
menu_labels_style.xml:
menu_labels_style.xml:
<style name="menu_labels_style">
<item name="android:background">@drawable/fab_label_background</item>
<item name="android:textColor">@color/white</item>
</style>
fab_label_background.xml:
fab_label_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/black_semi_transparent"/>
<padding
android:left="16dp"
android:top="4dp"
android:right="16dp"
android:bottom="4dp"/>
<corners
android:radius="2dp"/>
</shape>
Enjoy!
享受!
回答by Lukas
Google now provides an official library, called design library, containing the Fab Button. Just add the following Gradle dependency:
Google 现在提供了一个官方库,称为 design library,其中包含 Fab Button。只需添加以下 Gradle 依赖项:
compile 'com.android.support:design:22.2.0'
Afterwards you can use the fab button like this:
之后,您可以像这样使用 fab 按钮:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
More information can be found in their announcement
更多信息可以在他们的公告中找到
http://android-developers.blogspot.ch/2015/05/android-design-support-library.html
http://android-developers.blogspot.ch/2015/05/android-design-support-library.html
or on the javadoc page
或在 javadoc 页面上
http://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html
http://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html