问题描述
我尝试为自定义按钮添加高度,但结果是:
I tried to add elevation to my custom button but the result is:
如您所见,阴影被剪掉了.我曾尝试在 StackOverflow 和 google 中搜索,但发现了类似的问题,但没有答案:)
As you can see the shadow is clipped. I have tried to search in StackOverflow and google and I found similar question but no answers :)
XML:
<Button
android:id="@+id/email_sign_in_button"
android:layout_width="141dp"
android:layout_height="45dp"
android:textAlignment="gravity"
android:text="@string/action_sign_in"
android:gravity="center_horizontal"
android:elevation="4dp" />
这是我的自定义按钮样式.
Here is my custom button styles.
button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@drawable/button_disabled" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/button_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/button_regular" />
<item
android:state_enabled="true"
android:drawable="@drawable/button_regular" />
</selector>
未按下时的样式.
button_regular.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="-90"
android:centerX="10"
android:centerY="10"
android:endColor="#30bbff"
android:gradientRadius="10"
android:startColor="#0081c0"
android:type="linear"/>
<stroke android:width="5dip" android:color="#ffffff" />
<corners android:radius="20dip"/>
<padding android:left="7dp"
android:top="7dp"/>
</shape>
推荐答案
您的阴影可能会被 View 的边界剪掉.尝试在按钮底部添加填充.
Your shadows may be getting clipped by the View's bounds. Try adding padding to the bottom of the button.
如果按钮位于父级的底部,则父级 ViewGroup 也可能会裁剪阴影.确保父级具有填充并在父级上设置 android:clipToPadding="false"
.
If the button sits at the bottom of the parent, the parent ViewGroup may also be clipping the shadow. Make sure the parent has padding and set android:clipToPadding="false"
on the parent.
这篇关于高程阴影被剪裁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!