无法显示按钮阴影。
将我的代码简化为最小的示例:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/main_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp">
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:elevation="10dp"
android:translationZ="10dp"
android:background="@android:color/holo_green_light"
android:text="BUTTON"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
我需要那个布局结构。
主活动.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
阴影在 Android Studio 设计器中可见:
但在运行时不显示:
测试于:
- Genymotion 谷歌 Nexus 5X - 棉花糖 6.0.0 - API 23
- Genymotion 谷歌 Nexus 5 - 棒棒糖 5.1.0 - API 22
- Genymotion 三星 Galaxy S2 - Jelly Bean 4.1.1 - API 16
- 硬件设备,Samsung Galaxy S5 mini - KitKat 4.4.2 - API 19
结果都一样。
我在用着:
- 安卓工作室 2.1.2
- minSdkVersion 16
- targetSdk版本 24
请在 Andriod Studio 中创建一个新项目,Empty Activity 模板,然后将该代码复制并粘贴到 activity_main.xml
和 MainActivity.java
以便您可以测试并告诉我。
原文由 Ramiro 发布,翻译遵循 CC BY-SA 4.0 许可协议
从这里复制
只需将此属性添加到您的
Button
。您可以使用android:stateListAnimator
属性自行设置。完整代码:
更新 :
为了理解我把它设置为 10dp..
代码: