英文:
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
<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>
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:
<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>
答案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
.
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Button" />
Also do not forget to change theme Theme.AppCompat.Light.DarkActionBar
to Theme.MaterialComponents.Light.DarkActionBar
in styles.xml
file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论