我对材料设计的按钮样式感到困惑 . 我想得到彩色凸起的按钮,如附带的链接,就像使用部分下面的“强制停止”和“卸载”按钮一样 . 有可用的样式还是我需要定义它们?
http://www.google.com/design/spec/components/buttons.html#buttons-usage
我找不到默认的按钮样式 .
例:
<Button style="@style/PrimaryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:id="@+id/button3"
android:layout_below="@+id/editText5"
android:layout_alignEnd="@+id/editText5"
android:enabled="true" />
如果我尝试通过添加更改按钮的背景颜色
android:background="@color/primary"
所有样式都消失了,比如触摸动画,阴影,圆角等 .
11 回答
我将添加我的答案,因为我不使用任何其他答案 .
使用Support Library v7,所有样式实际上已经定义并可以使用,对于标准按钮,所有这些样式都可用:
Widget.AppCompat.Button
:Widget.AppCompat.Button.Colored
:Widget.AppCompat.Button.Borderless
Widget.AppCompat.Button.Borderless.Colored
:要回答这个问题,因此使用的风格是
如何更改颜色
For the whole app:
所有UI控件(不仅是按钮,还有浮动操作按钮,复选框等)的颜色由属性
colorAccent
管理,如here所述 . 您可以修改此样式并在主题定义中应用自己的颜色:For a specific button:
如果需要更改特定按钮的样式,可以定义新样式,继承上述父样式之一 . 在下面的示例中,我只更改了背景和字体颜色:
然后你只需要在按钮上应用这个新样式:
要在布局中设置默认按钮设计,请将此行添加到styles.xml主题:
其中
@style/btn
是您的按钮主题 . 这将为具有特定主题的布局中的所有按钮设置按钮样式最简单的解决方案
步骤1:使用最新的支持库
步骤2:使用AppCompatActivity作为父Activity类
步骤3:在布局XML文件中使用app命名空间
步骤4:使用AppCompatButton而不是Button
如果我理解正确,你想做这样的事情:
在这种情况下,它应该足以使用:
或者对于小于21的API:
除了Using Material Theme Tutorial .
动画变体是here .
这就是我得到我想要的东西 .
首先,制作一个按钮(在
styles.xml
中):按钮的波纹和背景,作为drawable
primary_round.xml
:这增加了我正在寻找的涟漪效应 .
这是一个示例,有助于在您的应用中一致地应用按钮样式 .
这是我使用特定样式的主题示例..
这就是我在res / drawable-v21文件夹中定义按钮形状和效果的方法......
2dp角落是为了使其与Material主题保持一致 .
使用新的Support Library 28.0.0-alpha1 release,设计库现在包含
Material Button
.您可以将此按钮添加到我们的布局文件中:
默认情况下,此类将使用主题的 accent colour 作为按钮填充背景颜色,白色使用按钮文本颜色 .
您可以使用以下属性自定义按钮:
app:rippleColor
:用于按钮波纹效果的颜色app:backgroundTint
:用于将色调应用于按钮的背景 . 如果要更改按钮的背景颜色,请使用此属性而不是背景 .app:strokeColor
:用于按钮笔划的颜色app:strokeWidth
:用于按钮笔划的宽度app:cornerRadius
:用于定义用于按钮角落的半径我尝试了很多答案和第三方库,但是没有一个保留边框并且对棒棒糖前端产生影响,同时对棒棒糖产生连锁反应没有缺点 . 这是我的最终解决方案,结合了几个答案(由于灰度颜色深度,边框/凸起在gif上没有很好地渲染):
Lollipop
Pre-lollipop
build.gradle
layout.xml
drawable-v21/btn_bg.xml
drawable/btn_bg.xml
Activity's onCreate
在
android.support.design.button.MaterialButton
旁边(Gabriele Mariotti提到),还有另一个名为com.google.android.material.button.MaterialButton的
Button
小部件,它具有不同的样式并从AppCompatButton
扩展:Filled, elevated Button (default) :
Filled, unelevated Button :
Text Button :
Icon Button :
A text Button with an icon: :
Read: https://material.io/develop/android/components/material-button/
1)您可以通过定义xml drawable来创建 rounded corner button ,您可以增加或减少半径以增加或减少按钮角的圆度 . 将此xml drawable设置为按钮的背景 .
2)要在按钮状态之间更改默认阴影和阴影过渡动画,您需要使用android:stateListAnimator属性定义选择器并将其应用于按钮 . 有关完整按钮自定义参考:http://www.zoftino.com/android-button
我刚刚创建了一个安卓库,可以让你轻松修改按钮颜色和波纹颜色
https://github.com/xgc1986/RippleButton
您不需要为每个想要具有不同颜色的按钮创建样式,从而允许您随机自定义颜色
您可以通过向视图添加z轴为视图提供航空,并且可以为其提供默认阴影 . 此功能在L预览中提供,并在发布后可用 . 现在,您可以简单地添加一个图像,为按钮背景提供此外观