Android材料设计按钮 - 前棒棒糖

时间:2014-11-07 15:43:14

标签: android button material-design

如何实现"凸起按钮"和#34;平面按钮"如谷歌的材料设计指南中所述?


  

凸起的按钮为大多数平面布局添加尺寸。他们强调>   繁忙或宽阔的空间。

raised buttons


  

使用工具栏和对话框的平面按钮来避免过度分层。

flat buttons

来源:http://www.google.com/design/spec/components/buttons.html

8 个答案:

答案 0 :(得分:121)

这需要Android 5.0

提升按钮

从Widget.Material.Button继承你的按钮样式,将自动应用标准的提升和提升动作。

<style name="Your.Button" parent="android:style/Widget.Material.Button">
    <item name="android:background">@drawable/raised_button_background</item>
</style>

然后你需要在波纹标记内创建一个带有按钮背景颜色的raised_button_background.xml文件:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/colorControlHighlight">
    <item android:drawable="@color/button_color"/>
</ripple>

平面按钮

编辑:您应该遵循Stephen Kaiser提供的建议,而不是我以前对平面按钮的建议:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="DONE"
    style="?android:attr/borderlessButtonStyle"
/>

编辑:如果您使用支持库,则可以使用style="?attr/borderlessButtonStyle"在Pre-Lollipop设备上获得相同的结果。 (注意没有android:)上面的例子变成了

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="DONE"
    style="?attr/borderlessButtonStyle"
/>

答案 1 :(得分:84)

要实现平面按钮,您只需添加style="?android:attr/borderlessButtonStyle"即可。

示例:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="DONE"
    style="?android:attr/borderlessButtonStyle"
    />

Here's the reference表示该属性。

答案 2 :(得分:4)

您可以使用MaterialDesignLibrary。它是第三方图书馆。

这是一个包含Android L组件的库,可以在android 2.2中使用 如果你想使用这个库,你只需要下载MaterialDesign项目,将它导入你的工作区,并将项目作为一个库添加到你的android项目设置中。

答案 3 :(得分:2)

我使用它作为AppCompat按钮的背景,它描绘了一个凸起的按钮(涟漪全部),希望它有所帮助。

myRaisedButton.xml - 在drawable文件夹中:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
               android:shape="rectangle">
            <solid android:color="@color/yourColor"/>
            <corners android:radius="6dp"/>
        </shape>
    </item>
    <item android:drawable="?android:selectableItemBackground"/>
</layer-list>

<强> styles.xml

<resources>

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

</resources>

styles.xml(v21)

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

<强> layout.xml

...
android:background="@drawable/myRaisedButton"

答案 4 :(得分:2)

我正在处理材料兼容性库。按钮类在那里,支持动画阴影和触摸波纹。也许你会发现它很有用。这是link

答案 5 :(得分:1)

有关如何使用appcompat libray执行此操作的说明,请查看我对其他问题的回答:https://stackoverflow.com/a/27696134/1932522

这将显示如何使用Android 5.0及更早版本的升级和平面按钮(最高为API级别11)!

答案 6 :(得分:1)

你要求材料设计按钮库 - 你明白了 - https://github.com/navasmdc/MaterialDesignLibrary

答案 7 :(得分:0)

您可能还需要在按钮上添加下边距,以便能够看到凸起按钮阴影效果:

<item name="android:layout_marginBottom">@dimen/activity_vertical_margin</item>
<item name="android:elevation">1dp</item>