我对材质设计的纽扣款式很困惑。我想要像附件链接那样的彩色凸起按钮。,比如用法部分下面的“强制停止”和“卸载”按钮。是否有可用的样式或者我需要定义它们?
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"
所有的样式都消失了,比如触摸动画、阴影、圆角等等。
我刚刚创建了一个android库,它允许您轻松地修改按钮颜色和波纹颜色
https://github.com/xgc1986/RippleButton
<com.xgc1986.ripplebutton.widget.RippleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Android button modified in layout"
android:textColor="@android:color/white"
app:buttonColor="@android:color/black"
app:rippleColor="@android:color/white"/>
你不需要为每个你想要不同颜色的按钮创建一个样式,允许你随机自定义颜色
简单的解决方案
步骤1:使用最新的支持库
compile 'com.android.support:appcompat-v7:25.2.0'
步骤2:使用AppCompatActivity作为你的父Activity类
public class MainActivity extends AppCompatActivity
步骤3:在布局XML文件中使用app命名空间
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
第四步:使用AppCompatButton而不是Button
<android.support.v7.widget.AppCompatButton
android:id="@+id/buttonAwesome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Awesome Button"
android:textColor="@color/whatever_text_color_you_want"
app:backgroundTint="@color/whatever_background_color_you_want"/>
我刚刚创建了一个android库,它允许您轻松地修改按钮颜色和波纹颜色
https://github.com/xgc1986/RippleButton
<com.xgc1986.ripplebutton.widget.RippleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Android button modified in layout"
android:textColor="@android:color/white"
app:buttonColor="@android:color/black"
app:rippleColor="@android:color/white"/>
你不需要为每个你想要不同颜色的按钮创建一个样式,允许你随机自定义颜色
我是这样得到我想要的东西的。
首先,创建一个按钮(在styles.xml中):
<style name="Button">
<item name="android:textColor">@color/white</item>
<item name="android:padding">0dp</item>
<item name="android:minWidth">88dp</item>
<item name="android:minHeight">36dp</item>
<item name="android:layout_margin">3dp</item>
<item name="android:elevation">1dp</item>
<item name="android:translationZ">1dp</item>
<item name="android:background">@drawable/primary_round</item>
</style>
按钮的波纹和背景,作为一个可绘制的primary_round.xml:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/primary_600">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1dp" />
<solid android:color="@color/primary" />
</shape>
</item>
</ripple>
这增加了我正在寻找的连锁反应。