如何为按钮添加动画/样式?Android Studio

huangapple go评论64阅读模式
英文:

How do I add animation / styling to a button? Android Studio

问题

我有一个按钮

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:onClick="loginIsClicked"
    android:layout_marginStart="8dp"
    android:layout_marginTop="12dp"
    android:layout_marginEnd="8dp"
    android:background="@drawable/button"
    android:text="@string/Auth"
    android:textAllCaps="false"
    android:textColor="@color/white"
    android:textSize="16sp"
    android:enabled="false" />


<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/red"/>
    <corners android:radius="10dp"/>
</shape>

如何在默认按钮中为按钮添加动画?

示例:
查看图片描述

英文:

I have a button

&lt;Button
    android:id=&quot;@+id/button&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;45dp&quot;
    android:onClick=&quot;loginIsClicked&quot;
    android:layout_marginStart=&quot;8dp&quot;
    android:layout_marginTop=&quot;12dp&quot;
    android:layout_marginEnd=&quot;8dp&quot;
    android:background=&quot;@drawable/button&quot;
    android:text=&quot;@string/Auth&quot;
    android:textAllCaps=&quot;false&quot;
    android:textColor=&quot;@color/white&quot;
    android:textSize=&quot;16sp&quot;
    android:enabled=&quot;false&quot; /&gt;


&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;shape android:shape=&quot;rectangle&quot; xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    &lt;solid android:color=&quot;@color/red&quot;/&gt;
    &lt;corners android:radius=&quot;10dp&quot;/&gt;
&lt;/shape&gt;

How can I add animation to the button used in the default buttons?

Example:
enter image description here

答案1

得分: 0

如果我理解正确,您正在寻找的是一个 ripple(涟漪)效果。请按以下方式将您的形状包裹在一个涟漪中,并提供涟漪颜色:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/rippleColor">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/red"/>
            <corners android:radius="10dp"/>
        </shape>
    </item>
</ripple>
英文:

If I understood correctly, what you are looking for is a ripple. Wrap your shape in a ripple as follows and provide your ripple color:

&lt;ripple xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:color=&quot;@color/rippleColor&quot;&gt;
    &lt;item&gt;
        &lt;shape android:shape=&quot;rectangle&quot;&gt;
            &lt;solid android:color=&quot;@color/red&quot;/&gt;
            &lt;corners android:radius=&quot;10dp&quot;/&gt;
        &lt;/shape&gt;
    &lt;/item&gt;
&lt;/ripple&gt;

答案2

得分: 0

添加 MaterialButton

<com.google.android.material.button.MaterialButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Button" />

同时不要忘记在 styles.xml 文件中将主题从 Theme.AppCompat.Light.DarkActionBar 更改为 Theme.MaterialComponents.Light.DarkActionBar

英文:

Add MaterialButton.

&lt;com.google.android.material.button.MaterialButton
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_margin=&quot;16dp&quot;
        android:text=&quot;Button&quot; /&gt;

Also do not forget to change theme Theme.AppCompat.Light.DarkActionBar to Theme.MaterialComponents.Light.DarkActionBar in styles.xml file.

huangapple
  • 本文由 发表于 2020年10月15日 02:15:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64359263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定