【问题标题】:[Android L]Android Material circular button[Android L]Android Material 圆形按钮
【发布时间】:2014-08-19 06:48:15
【问题描述】:

http://imgur.com/KafNkf8

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

【问题讨论】:

标签: android xml android-5.0-lollipop


【解决方案1】:

这个按钮叫做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 L 出现时,Google 会发布一个兼容性库!
  • 有没有人尝试为ripple.xml 使用替代drawable,用于pre L 版本?我在定义 drawable 和 drawable-v21 文件夹时遇到了问题。在 drawable 中,我使用 shape 和 drawable-21,我使用了波纹,但我的 Ripple.xml 永远不会出现在 L 版本上...
  • 您好 Sandra,Google 发布的预览版 SDK 绝不是用于生产,如果您设法在以前版本的 android 中启用 android L 主题/功能,那将是非常快速且肮脏/骇客的方法.当然,如果你想实现未完成的 API,这取决于你。当我开始开发我的应用程序时,我非常渴望使用新的 API,考虑到我对应用程序的体验,我不得不恢复到较低的 API,因为 android L 还没有准备好。我建议遵循 Google 的设计指南link
  • 只需实施 Google 的材料设计指南,但不要使用新的 API。我建议大家不要使用 API 20,直到 API 完全准备好投入生产。无论如何,我很确定 Google 发布了预览版 SDK,因此开发人员可以了解即将推出的内容。我希望我能帮上忙。 -猴子
  • 如果我希望整个按钮是一个带有波纹的圆形可绘制对象(位图本身是一个圆形)怎么办?
【解决方案2】:

有一种更简单且适用于所有版本 (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" />

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

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

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

对于 Android 棒棒糖是:

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

圆形可绘制的形状是:

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

它适用于所有安卓版本,它会自动在 Lollipop 中生成圆形阴影。

【讨论】:

    【解决方案3】:

    你需要为它定义另一个drawable。它实际上很简单,但没有记录。

    <?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>
    

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

    别忘了设置高程和轮廓

    【讨论】:

      猜你喜欢
      • 2012-03-09
      • 1970-01-01
      • 2012-02-28
      • 2017-10-01
      • 1970-01-01
      • 2017-09-11
      • 1970-01-01
      相关资源
      最近更新 更多