android - [Android L]Android Material 圆形按钮

标签 android xml android-5.0-lollipop

http://imgur.com/KafNkf8 Android L

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

最佳答案

这个按钮叫做FAB(Floating Action Button),制作起来很简单。

Activity.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);
    }

}

动画.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>

和你的布局文件来定义 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>

最后,当您按下按钮时会产生涟漪效应。 波纹.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>

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

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

关于android - [Android L]Android Material 圆形按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24464017/

相关文章:

java - 是否可以生成类的内部类以使用注释处理器进行编译?

Android XML 格式不正确

Android Material Design View State 变化与 Elevation

android - Lollipop中的DialogFragment按钮颜色变化

Android 5.0 Toolbar fragment backstack

android - 在base64中编码图像并发送到服务器

android - 在空上调用了 '_debugTypesAreRight'方法

android - 无法使用java mail api从android应用程序发送邮件

c# - 显示前检查空 XML 数据验证

Java:JDOM2在xml中写入ascii字符