[Android L]Android Material圆形按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24464017/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:13:22  来源:igfitidea点击:

[Android L]Android Material circular button

androidxmlandroid-5.0-lollipop

提问by M0NKEYRASH

http://imgur.com/KafNkf8Android L

http://imgur.com/KafNkf8安卓L

So, how would someone go about implementing the circular button that is showcased in android L? Also what is the technical name of the the circular button, XML code would be appreciated.

那么,有人将如何实现在 android L 中展示的圆形按钮?另外圆形按钮的技术名称是什么,XML 代码将不胜感激。

回答by M0NKEYRASH

The button is called a FAB(Floating Action Button), it's quite simple to make.

该按钮称为 FAB(浮动操作按钮),制作起来非常简单。

Activity.java

活动.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layoutfab);

        //Outline
        int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
        Outline outline = new Outline();
        outline.setOval(0, 0, size, size);
        findViewById(R.id.fab).setOutline(outline);
    }

}

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

尺寸文件

<resources>
    <dimen name="fab_size">56dp</dimen>

    <dimen name="button_elevation">2dp</dimen>
    <dimen name="button_press_elevation">4dp</dimen>
</resources>

and your layout file to define the FAB button.

和您的布局文件来定义 FAB 按钮。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageButton
        android:id="@+id/fab"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_width="@dimen/fab_size"
        android:layout_height="@dimen/fab_size"
        android:layout_gravity="bottom|right"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:background="@drawable/ripple"
        android:stateListAnimator="@anim/anim"
        android:src="@drawable/ic_action_add"
        android:elevation="4dp"
        />


</RelativeLayout>

Finally the ripple affect when you press the button. ripple.xml

最后,当您按下按钮时,涟漪会影响。涟漪文件

<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
    <item>
        <shape android:shape="oval">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>

Enjoy your new FAB. Please keep in mind this is for android L only!

享受您的新 FAB。请记住,这仅适用于 android L!

EDITThis has been updated at: https://stackoverflow.com/a/24548910/4352176

编辑这已更新:https: //stackoverflow.com/a/24548910/4352176

回答by Jose Ma

There is a easier and for all version (v7) way:

有一种更简单且适用于所有版本 (v7) 的方法:

<ImageButton style="@style/AppTheme"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:text="New Button"
    android:id="@+id/OkBt"
    android:elevation="10dp"
    android:src="@drawable/ic_keyboard_return_black_36dp"
    android:background="@drawable/circle" />

Look at style="@style/AppTheme" The style that I have set for old version is:

看 style="@style/AppTheme" 我给老版本设置的样式是:

<style name="AppTheme" parent="AppTheme.Base"/>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">

For Android lollipop is:

对于 Android 棒棒糖是:

<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">

The shape drawable for circle is:

圆形可绘制的形状是:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
    <solid android:color="#81D4FA"/>
</shape>

It works fine for all android version, it make automatically the circle shadow in Lollipop.

它适用于所有 android 版本,它会自动在 Lollipop 中制作圆形阴影。

回答by nickmartens1980

You need to define another drawable for it. It is quite simple actually, but undocumented.

您需要为它定义另一个可绘制对象。这实际上很简单,但没有记录。

<?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="oval">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>

I got this from here: https://github.com/romainguy/google-io-2014/blob/7c174c7901d8cd2601807dcd17f3df7ed88fbee9/app/src/main/res/drawable/info_background.xml

我从这里得到这个:https: //github.com/romainguy/google-io-2014/blob/7c174c7901d8cd2601807dcd17f3df7ed88fbee9/app/src/main/res/drawable/info_background.xml

Don't forget to set the elevation and the outline

不要忘记设置高程和轮廓