我做了圆角按钮。但是当它们被按下时,它们不会给人一种被按压的感觉。我想将这个效果添加到我的按钮上,当它们被点击时,它们会给人一种按下/点击的感觉。 我在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>
答案 0 :(得分:1)
您可以在android:state
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>
为:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The normal state of the button -->
<item android:state_pressed="false">
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
<stroke android:color="#ff0000" android:width="1dp" />
<!-- make the button corners rounded-->
<corners android:radius="25dp"/>
</shape>
</item>
<!-- The state when the button is being pressed or clicked. -->
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="@android:color/holo_red_dark"/>
<!--apply a border around button-->
<stroke android:color="#ff0000" android:width="1dp" />
<!-- make the button corners rounded-->
<corners android:radius="25dp"/>
</shape>
</item>
</selector>
单击时为按钮提供振动效果。加上这个
<uses-permission android:name="android.permission.VIBRATE"/>
您AndroidManifest.xml
的权限 并对你的活动这样做。
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
final Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
Button button = findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Set the milliseconds of vibration you want.
vibrator.vibrate(500);
}
});
}
答案 1 :(得分:0)
表示您在xml
中创建的drawable
制作按钮矩形
将此行添加到该xml
<item android:state_pressed="true">
答案 2 :(得分:0)
使用前景属性:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_rounded_corners_gradient"
android:foreground="?android:selectableItemBackground" />
答案 3 :(得分:0)
你应该尝试按下按钮上的波浪效果的波纹,以便按下按钮感觉;)
只需创建一个新的可绘制文件并将此文件设置为按钮的背景...
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimary">
</ripple>
希望有所帮助