我想要去掉按钮上的阴影,使其看起来更加扁平。
现在的样式如下图所示:
但我想要的效果是这样的:
另一种选择是添加
style="?android:attr/borderlessButtonStyle"
按照这里文档所述,将其添加到您的按钮XML中。
例如:
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"
style="?android:attr/borderlessButtonStyle" />
- Tobliug一个更简单的方法是在你的按钮中添加这个标签:
android:stateListAnimator="@null"
尽管需要API级别21或更高版本...
item
? - QuarKstateListAnimator = null
Java
setStateListAnimator(null);
XML
android:stateListAnimator="@null"
<style name="MyButtonStyle" parent="@style/Widget.AppCompat.Button.Borderless"></style>
别忘了添加
<item name="android:textAllCaps">false</item>
否则按钮文本将会是大写字母形式。添加 Material Design 样式的按钮 xml 代码如下:
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
android:stateListAnimator="@null"
需要 API 级别 21
android:outlineProvider="none"
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape android:shape="rectangle">
<solid android:color="@color/app_theme_light" />
<padding
android:left="8dp"
android:top="4dp"
android:right="8dp"
android:bottom="4dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@color/app_theme_dark" />
<padding
android:left="8dp"
android:top="4dp"
android:right="8dp"
android:bottom="4dp" />
</shape>
</item>
</selector>
以上所有答案都很好,但是我建议另一个选项:
<style name="FlatButtonStyle" parent="Base.Widget.AppCompat.Button">
<item name="android:stateListAnimator">@null</item>
<!-- more style custom here -->
</style>
@Alt-Cat的答案对我有用!
R.attr.borderlessButtonStyle中不包含阴影。
并且button的文档很棒。
此外,您可以在第二个构造函数中为自定义按钮设置此样式。
public CustomButton(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.borderlessButtonStyle);
}