Android材质设计按钮样式

Android材质设计按钮样式,android,material-design,android-button,Android,Material Design,Android Button,我对材料设计的按钮样式感到困惑。我想得到彩色的凸起按钮,如附件中的链接,如“强制停止”和“卸载”按钮下看到的使用部分。是否有可用的样式或我需要定义它们 我找不到默认的按钮样式 例如: <Button style="@style/PrimaryButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Calculate" an

我对材料设计的按钮样式感到困惑。我想得到彩色的凸起按钮,如附件中的链接,如“强制停止”和“卸载”按钮下看到的使用部分。是否有可用的样式或我需要定义它们

我找不到默认的按钮样式

例如:

 <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" />

所有样式都会消失,例如触摸动画、阴影、圆角等。

您可以通过向视图添加z轴来赋予视图航空感,并且可以将默认阴影添加到视图中。此功能在L preview中提供,并将在发布后可用。现在,您只需添加一个图像,即可为按钮背景提供此外观

以下是我获得所需内容的方式

首先,制作一个按钮(在
styles.xml
中):


这增加了我一直在寻找的连锁反应。

如果我理解正确,你会想这样做:

在这种情况下,只需使用:

<item name="android:colorButtonNormal">#2196f3</item>
#2196f3
或API小于21时:

<item name="colorButtonNormal">#2196f3</item>
#2196f3
除此之外


动画变体是。

以下是一个示例,它将有助于在整个应用程序中一致地应用按钮样式

下面是我在特定样式中使用的示例主题

<style name="MyTheme" parent="@style/Theme.AppCompat.Light">
   <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:buttonStyle">@style/ButtonAppTheme</item>
</style>
<style name="ButtonAppTheme" parent="android:Widget.Material.Button">
<item name="android:background">@drawable/material_button</item>
</style>

@颜色/原色
@颜色/原色/深色
@颜色/口音
@风格/主题
@可拉伸/材质按钮
这就是我在res/drawable-v21文件夹中定义按钮形状和效果的方式

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/colorControlHighlight">
  <item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
      <corners android:radius="2dp" /> 
      <solid android:color="@color/primary" />
    </shape>
  </item>
</ripple>


2dp角点是为了使其与材质主题保持一致。

我刚刚创建了一个android库,允许您轻松修改按钮颜色和波纹颜色



你不需要为你想要的每个按钮创建一个不同颜色的样式,允许你随机定制颜色

我尝试了很多答案和第三方LIB,但没有一个能在棒棒糖前保持边框和提升效果,同时对棒棒糖产生涟漪效应。以下是我的最终解决方案,结合了几个答案(由于灰度颜色深度的原因,GIF上的边框/凸起未得到很好的渲染):

棒棒糖

棒棒糖前

build.gradle

compile 'com.android.support:cardview-v7:23.1.1'
    final CardView cardView = (CardView) findViewById(R.id.card);
    final Button button = (Button) findViewById(R.id.button);
    button.setOnTouchListener(new View.OnTouchListener() {
        ObjectAnimator o1 = ObjectAnimator.ofFloat(cardView, "cardElevation", 2, 8)
                .setDuration
                        (80);
        ObjectAnimator o2 = ObjectAnimator.ofFloat(cardView, "cardElevation", 8, 2)
                .setDuration
                        (80);

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    o1.start();
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    o2.start();
                    break;
            }
            return false;
        }
    });
layout.xml

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card"
    card_view:cardElevation="2dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardMaxElevation="8dp"
    android:layout_margin="6dp"
    >
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="0dp"
        android:background="@drawable/btn_bg"
        android:text="My button"/>
</android.support.v7.widget.CardView>
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/colorControlHighlight">
    <item android:drawable="?attr/colorPrimary"/>
</ripple>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorPrimaryDark" android:state_pressed="true"/>
    <item android:drawable="@color/colorPrimaryDark" android:state_focused="true"/>
    <item android:drawable="@color/colorPrimary"/>
</selector>

我将添加我的答案,因为我不使用提供的任何其他答案

对于支持库v7,所有样式实际上都已定义并准备好使用,对于标准按钮,所有这些样式都可用:

style="@style/Widget.AppCompat.Button"
style="@style/Widget.AppCompat.Button.Colored"
style="@style/Widget.AppCompat.Button.Borderless"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
Widget.AppCompat.Button

Widget.AppCompat.Button.Colored

Widget.AppCompat.Button.Borderless

Widget.AppCompat.Button.Borderless.Colored


因此,要回答这个问题,使用的样式是

<Button style="@style/Widget.AppCompat.Button.Colored"
.......
.......
.......
android:text="Button"/>
对于特定按钮:

如果需要更改特定按钮的样式,可以定义新样式,继承上述父样式之一。在下面的示例中,我只是更改了背景和字体颜色:

<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
    <item name="colorButtonNormal">@color/Red</item>
    <item name="android:textColor">@color/White</item>
</style>
要在布局中设置默认按钮设计,请将此行添加到styles.xml主题:

<item name="buttonStyle">@style/btn</item>
@style/btn
其中
@style/btn
是您的按钮主题。这将为具有特定主题的布局中的所有按钮设置按钮样式

最简单的解决方案
步骤1:使用最新的支持库
第2步:将AppCompatActivity用作父活动类
步骤3:在布局XML文件中使用应用程序名称空间


步骤4:使用AppCompatButton而不是Button
1)您可以通过定义xml drawable来创建圆角按钮,还可以增加或减少半径以增加或减少按钮角的圆度。
将此xml可绘制设置为按钮的背景

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="4dp"
    android:insetTop="6dp"
    android:insetRight="4dp"
    android:insetBottom="6dp">
    <ripple android:color="?attr/colorControlHighlight">
        <item>
            <shape android:shape="rectangle"
                android:tint="#0091ea">
                <corners android:radius="10dp" />
                <solid android:color="#1a237e" />
                <padding android:bottom="6dp" />
            </shape>
        </item>
    </ripple>
</inset>

2) 要更改按钮状态之间的默认阴影和阴影过渡动画,需要定义选择器并使用android:stateListAnimator属性将其应用于按钮。有关完整的按钮自定义参考:

您可以使用

添加到您的
build.gradle

dependencies { implementation ‘com.google.android.material:material:1.3.0’ }
然后将以下内容添加到布局中:

<com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.OutlinedButton" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        app:strokeColor="@color/colorAccent"
        app:strokeWidth="6dp"
        app:layout_constraintStart_toStartOf="parent"
        app:shapeAppearance="@style/MyShapeAppearance"
   />
  • 在我看来,这将是最好的选择。如果要覆盖默认样式中的某些主题属性,则可以使用新的
    材质ThemeOverlay
    属性
  • 比如:

    <style name="MyButtonStyle"
     parent="Widget.MaterialComponents.Button">
        <item name="backgroundTint">@color/button_selector</item>
        //..
    </style>
    
    <style name="MyButtonStyle"
     parent="Widget.MaterialComponents.Button">
       <item name=“materialThemeOverlay”>@style/GreenButtonThemeOverlay</item>
    </style>
    
    <style name="GreenButtonThemeOverlay">
      <!-- For filled buttons, your theme's colorPrimary provides the default background color of the component --> 
      <item name="colorPrimary">@color/green</item>
    </style>
    
    默认情况下,该类将使用主题的强调色作为按钮填充的背景色,同时使用白色作为按钮文本颜色

    您可以使用以下属性自定义按钮:

    • 应用程序:rippleColor
      : 用于按钮波纹效果的颜色


    • app:backgroundTint
      : 用于将色调应用于按钮的背景。如果要更改按钮的背景色,请使用此属性而不是背景

    • app:strokeColor
      :用于按钮笔划的颜色

    • app:strokeWidth
      : 用于按钮笔划的宽度

    • app:cornerRadius
      : 用于定义用于按钮角的半径


    安卓.支持.设计.按钮.材料按钮旁边的
    (由加布里埃尔·马里奥蒂提到)

    还有另一个名为的
    按钮
    小部件,它具有不同的样式,并从
    AppCompatButton
    扩展而来:

    style="@style/Widget.MaterialComponents.Button"
    style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
    style="@style/Widget.MaterialComponents.Button.TextButton"
    style="@style/Widget.MaterialComponents.Button.Icon"
    style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
    
    填充、提升的
    按钮
    (默认)

    已填充、未提升的
    按钮

    文本
    按钮

    图标
    按钮

    带有图标的文本
    按钮


    compile 'com.android.support:appcompat-v7:25.2.0'
    
    public class MainActivity extends AppCompatActivity
    
    <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">
    
    <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"/>
    
    <?xml version="1.0" encoding="utf-8"?>
    <inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:insetLeft="4dp"
        android:insetTop="6dp"
        android:insetRight="4dp"
        android:insetBottom="6dp">
        <ripple android:color="?attr/colorControlHighlight">
            <item>
                <shape android:shape="rectangle"
                    android:tint="#0091ea">
                    <corners android:radius="10dp" />
                    <solid android:color="#1a237e" />
                    <padding android:bottom="6dp" />
                </shape>
            </item>
        </ripple>
    </inset>
    
    dependencies { implementation ‘com.google.android.material:material:1.3.0’ }
    
    <com.google.android.material.button.MaterialButton
            style="@style/Widget.MaterialComponents.Button.OutlinedButton" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            app:strokeColor="@color/colorAccent"
            app:strokeWidth="6dp"
            app:layout_constraintStart_toStartOf="parent"
            app:shapeAppearance="@style/MyShapeAppearance"
       />
    
    <style name="MyButtonStyle"
     parent="Widget.MaterialComponents.Button">
        <item name="backgroundTint">@color/button_selector</item>
        //..
    </style>
    
    <style name="MyButtonStyle"
     parent="Widget.MaterialComponents.Button">
       <item name=“materialThemeOverlay”>@style/GreenButtonThemeOverlay</item>
    </style>
    
    <style name="GreenButtonThemeOverlay">
      <!-- For filled buttons, your theme's colorPrimary provides the default background color of the component --> 
      <item name="colorPrimary">@color/green</item>
    </style>
    
    <android.support.design.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="YOUR TEXT"
        android:textSize="18sp"
        app:icon="@drawable/ic_android_white_24dp" />
    
    style="@style/Widget.MaterialComponents.Button"
    style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
    style="@style/Widget.MaterialComponents.Button.TextButton"
    style="@style/Widget.MaterialComponents.Button.Icon"
    style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
    
    style="@style/Widget.MaterialComponents.Button"
    
    style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
    
    style="@style/Widget.MaterialComponents.Button.TextButton"
    
    style="@style/Widget.MaterialComponents.Button.Icon"
    app:icon="@drawable/icon_24px" // Icons can be added from this
    
    // here is the custom button style
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:angle="45"
                android:centerColor="@color/colorPrimary"
                android:startColor="@color/colorPrimaryDark"
                android:endColor="@color/colorAccent"
                >
            </gradient>
            <corners
                android:topLeftRadius="10dp"
                android:topRightRadius="10dp"
                android:bottomLeftRadius="10dp"
                android:bottomRightRadius="10dp"
                >
            </corners>
            <stroke
                android:width="2dp"
                android:color="@color/colorWhite"
                >
              </stroke>
          </shape>
           </item>
    
    </selector>