Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓仰角未在按钮上显示阴影 无法使按钮阴影显示。_Java_Android_Android Studio_Button_Material Design - Fatal编程技术网

Java 安卓仰角未在按钮上显示阴影 无法使按钮阴影显示。

Java 安卓仰角未在按钮上显示阴影 无法使按钮阴影显示。,java,android,android-studio,button,material-design,Java,Android,Android Studio,Button,Material Design,将我的代码精简到最简单的示例: 活动\u 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" androi

将我的代码精简到最简单的示例:

活动\u 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>
阴影在Android Studio designer中可见:

但在运行时未显示:

测试日期:

  • Genymotion谷歌Nexus 5X-棉花糖6.0.0-API 23
  • Genymotion谷歌Nexus 5-棒棒糖5.1.0-API 22
  • Genymotion三星Galaxy S2-果冻豆4.1.1-API 16
  • 硬件设备,三星Galaxy S5 mini-KitKat 4.4.2-API 19
结果都一样

我正在使用:

  • Android Studio 2.1.2
  • 明斯克版本16
  • targetSdkVersion 24
请在Andriod Studio中创建一个新项目,清空活动模板,然后将代码复制并粘贴到
Activity\u main.xml
main Activity.java
中,以便您可以测试并告诉我

材质
下的默认
按钮
样式有一个
状态列表动画师
控制
android:elevation
android:translationZ
财产

只需将此属性添加到
按钮
。您可以使用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">
<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>

更新:

为了理解,我把它设为10dp

xml代码:

        <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">
<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>


它在我的情况下起作用,请尝试一下
app:elevation

<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:layout_topLeft="20dp"
    app:elevation="2dp"
    android:translationZ="2dp"
    android:background="@android:color/holo_green_light"
    android:text="BUTTON">


不起作用。你有没有在一个新的Android项目中尝试成为stateListAnimator?谢谢它起作用了!但它可以在任何API级别为21或更高的仿真器中工作。它不适用于API 20或更低版本。我将提出另一个关于低级API替代方案的问题。谢谢直接将您的按钮添加到ScrollView,然后查看它是否显示阴影。这可能是由于包裹的线性和相对性。检查此链接以供参考任何
视图
都没有此类属性
layout\u top left
@jankigadhiya是的,你是对的,在材料设计方面也是如此。android:elevation=“2dp”和
android:translationZ=“2dp”
只有在你设置
android:stateListAnimator=“@null”
时才有效。否则它是take deafult属性。是的,示例代码中有错误,左上方没有布局。编辑。还在结账。谢谢“意外命名空间前缀…”应用程序:前缀在buttonOh my god.中不可用。。您必须首先添加名称空间。如果你不知道,不要投反对票。请告诉我添加更多规格。我将对此进行解释。即使添加应用程序名称空间,默认的
stateListAnimator
也会覆盖这两个名称空间中的
elevation
。我在hurryWorks中写了前面的评论,就像一个符咒,谢谢:)