我想去掉按钮的阴影,使它看起来更平坦。
我现在有这个:
但我想要的是:
我想去掉按钮的阴影,使它看起来更平坦。
我现在有这个:
但我想要的是:
使用这个作为你的按钮的背景可能会有所帮助,根据你的需要改变颜色
<?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="?android:attr/borderlessButtonStyle"
到您的按钮xml,如这里所述 http://developer.android.com/guide/topics/ui/controls/button.html
一个例子是
<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" />
我使用自定义样式
<style name="MyButtonStyle" parent="@style/Widget.AppCompat.Button.Borderless"></style>
别忘了加上
<item name="android:textAllCaps">false</item>
否则按钮文本将是大写的。
@Alt-Cat的答案对我有用!
r.t r. borderlessbuttonstyle不包含阴影。
按钮的文档很棒。
同样,你也可以在你的自定义按钮中,在第二个构造函数中设置这个样式。
public CustomButton(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.borderlessButtonStyle);
}
科特林
stateListAnimator = null
Java
setStateListAnimator(null);
XML
android:stateListAnimator="@null"
您可以使用TextView代替按钮,并在java代码中添加单击侦听器。
I.e.
在活动布局xml中:
<TextView
android:id="@+id/btn_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:text="@string/btn_text"
android:gravity="center"
android:textColor="@color/colorAccent"
android:fontFamily="sans-serif-medium"
android:textAllCaps="true" />
在activity Java文件中:
TextView btnTextView = (TextView) findViewById(R.id.btn_text_view);
btnTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// handler code
}
});
材质设计按钮添加到按钮xml: 风格= " @style / Widget.MaterialComponents.Button.UnelevatedButton”
以上所有答案都很棒,但我将建议另一种选择:
<style name="FlatButtonStyle" parent="Base.Widget.AppCompat.Button">
<item name="android:stateListAnimator">@null</item>
<!-- more style custom here -->
</style>
带有自定义按钮;使用R.style。Widget_AppCompat_Button_Borderless, Kotlin way-
class CSButton @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = R.style.Widget_AppCompat_Button_Borderless
) : AppCompatButton(context, attrs, defStyleAttr)
这里没有提到,但如果你想要一个没有阴影的材质按钮,你可以通过创建以下自定义样式来实现:
<style name="materialButtonNoShadow" parent="Widget.MaterialComponents.Button.UnelevatedButton">
然后把它应用到你的xml布局中:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/noShadowButton"
style="?attr/materialButtonNoShadow"/>
更新材料组件
如果你之前使用过这样的东西:
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:buttonStyle">@style/NoShadowButton</item>
</style>
<style name="NoShadowButton" parent="Widget.AppCompat.Button">
<item name="android:stateListAnimator">@null</item>
</style>
和我一样,你也在挠头,想知道为什么你已经迁移到了Theme.MaterialComponents.Light。*,这是因为buttonStyle只引用AppCompat按钮样式-有一个新的样式materialButtonStyle。这解决了我的问题:
<style name="MyTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="materialButtonStyle">@style/Widget.MaterialComponents.Button.UnelevatedButton</item>
</style>
这里有一个很好的指南,告诉您还有哪些已经更改,以及如何正确迁移。