我製作了圓角按鈕。但是當他們被按下時,他們並沒有感受到被按下的感覺。我想將這種效果添加到我的按鈕中,當他們被點擊時,他們應該感覺到他們被按下/點擊。 我在我的drawable文件夾中創建了一個button_rounded_corners_gradient.xml文件。這個文件包含使我的按鈕圓角和矩形的圓角的代碼。那麼我在xml中的按鈕代碼的android:background屬性中使用這個文件。如何讓我的圓角按鈕有被按下的感覺?
activity_mainn.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.anonymous.fyplogin.MainActivity">
<Button
android:id="@+id/fbbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Login with Facebook "
android:background="@drawable/button_rounded_corners_gradient"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/phbutton"
app:layout_constraintVertical_bias="0.919" />
<Button
android:id="@+id/phbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Login with Phone "
android:background="@drawable/button_rounded_corners_gradient"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="71dp" />
</android.support.constraint.ConstraintLayout>
button_rounded_corners_gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<!--make a gradient background-->
<gradient
android:type="linear"
android:startColor="#003333"
android:endColor="#003333"
android:centerColor="#003333"
android:angle="90"
android:gradientRadius="90"
/>
<!--apply a border around button-->
<stroke android:color="#ff0000" android:width="2dp" />
<!-- make the button corners rounded-->
<corners android:radius="25dp"/>
</shape>
</item>
</selector>
試試這個https://stackoverflow.com/a/7176006/3364266 –
看看這篇文章https://開頭stacko versink.com/questions/3882064/how-to-change-color-of-button-in-android-when-clicked希望它能幫到你 –