Android 高度不显示按钮上的阴影

新手上路,请多包涵

无法显示按钮阴影。

将我的代码简化为最小的示例:

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.xmlMainActivity.java 以便您可以测试并告诉我。

原文由 Ramiro 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 820
2 个回答

The default Button style under Material has a StateListAnimator that controls the android:elevation and android:translationZ properties.

从这里复制

只需将此属性添加到您的 Button 。您可以使用 android:stateListAnimator 属性自行设置。

 android:stateListAnimator="@null"

完整代码:

         <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="2dp"
                android:translationZ="2dp"
                android:stateListAnimator="@null"
                android:background="@android:color/holo_green_light"
                android:text="BUTTON">

更新 :

为了理解我把它设置为 10dp..

代码:

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp">

        <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:stateListAnimator="@null"
            android:background="@android:color/holo_green_light"
            android:text="BUTTON"/>

    </RelativeLayout>

在此处输入图像描述

原文由 Harshad Pansuriya 发布,翻译遵循 CC BY-SA 3.0 许可协议

您需要设置 android:outlineProvider="bounds" 如果海拔不适用于您的视图!

(例如,我简化了下面的代码)

看法:

 <View
    android:id="@+id/view"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:outlineProvider="bounds"
    android:elevation="20dp" >

结果:

在此处输入图像描述

原文由 elfefe 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题