如何实现Google的材料设计指南中所述的“凸起按钮”和“扁平按钮”?
凸起的按钮为大多数平面布局增加了尺寸。他们强调在繁忙或宽敞的空间上的功能。
为工具栏和对话框使用平面按钮,以避免过多的分层。
来源:http://www.google.com/design/spec/components/buttons.html
如何实现Google的材料设计指南中所述的“凸起按钮”和“扁平按钮”?
凸起的按钮为大多数平面布局增加了尺寸。他们强调在繁忙或宽敞的空间上的功能。
为工具栏和对话框使用平面按钮,以避免过多的分层。
来源:http://www.google.com/design/spec/components/buttons.html
Answers:
这需要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"
/>
编辑:如果使用支持库,则可以在Pre-Lollipop设备上实现相同的结果style="?attr/borderlessButtonStyle"
。(注意缺少android:
),以上示例变为
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DONE"
style="?attr/borderlessButtonStyle"
/>
borderlessButtonStyle
中设置为@style/Widget.AppCompat.Button.Borderless
。如果希望它拉出强调色,请将borderlessButtonStyle
主题中的设置为@style/Widget.AppCompat.Button.Borderless.Colored
。
style="@style/Widget.AppCompat.Button.Borderless"
使用AppCompat库时
您可以使用MaterialDesignLibrary。它是第三方图书馆。
这是一个库,其中包含要在android 2.2中使用的Android L组件。如果要使用此库,则只需下载MaterialDesign项目,将其导入到工作区中,然后将该项目作为库添加到android项目设置中。
我正在研究材料兼容性库。按钮类在那里,并支持动画阴影和触摸波纹。也许您会发现它很有用。这是链接。
我将其用作AppCompat的按钮的背景,它描绘了一个凸起的按钮(所有n个波纹),希望能有所帮助。
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"
你问的材料设计按钮库-你得到它- https://github.com/navasmdc/MaterialDesignLibrary
您可能还需要在按钮上添加一个底边距,以便能够看到凸起的阴影效果:
<item name="android:layout_marginBottom">@dimen/activity_vertical_margin</item>
<item name="android:elevation">1dp</item>