Android高程未在Button上显示阴影 [英] Android elevation not showing shadow on Button

查看:109
本文介绍了Android高程未在Button上显示阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法显示按钮阴影.

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

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">< LinearLayoutandroid:id =" @ + id/main_layout"android:layout_width =" wrap_content"android:layout_height =" wrap_content">< RelativeLayoutandroid:layout_width =" match_parent"android:layout_height =" match_parent"android:layout_marginTop =" 10dp"android:layout_marginLeft =" 10dp"><按钮android:id ="@@ id/my_button"android:layout_width =" wrap_content"android:layout_height =" wrap_content"android:layout_centerHorizo​​ntal ="true"android:layout_marginLeft =" 20dp"android:elevation ="10dp"android:translationZ ="10dp"android:background =" @android:color/holo_green_light"android:text ="BUTTON"/></RelativeLayout></LinearLayout></ScrollView> 

我需要那种布局结构.

MainActivity.java

 公共类MainActivity扩展了AppCompatActivity {@Override受保护的void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}} 

阴影在Android Studio设计器中可见:

但在运行时未显示:

经过测试:

  • Genymotion Google Nexus 5X-棉花糖6.0.0-API 23
  • Genymotion Google 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
  • minSdkVersion 16
  • targetSdkVersion 24

请在Andriod Studio中的Empty Activity模板中创建一个新项目,然后将该代码复制并粘贴到 activity_main.xml MainActivity.java 中,以便进行测试并告知我.

解决方案

材料下的默认 Button 样式具有 StateListAnimator 控制 android:elevation android:translationZ 属性.

Can't get button shadows to show up.

Stripped down my code to the minimal example:

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>

I need that layout structure.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

Shadows is visible in Android Studio designer:

But not shown at runtime:

Tested on:

  • Genymotion Google Nexus 5X - Marshmallow 6.0.0 - API 23
  • Genymotion Google Nexus 5 - Lollipop 5.1.0 - API 22
  • Genymotion Samsung Galaxy S2 - Jelly Bean 4.1.1 - API 16
  • Hardware device, Samsung Galaxy S5 mini - KitKat 4.4.2 - API 19

All same result.

I'm using:

  • Android Studio 2.1.2
  • minSdkVersion 16
  • targetSdkVersion 24

Please create a new project in Andriod Studio, Empty Activity template, then copy and paste that code into activity_main.xml and MainActivity.java so you can test and tell me.

解决方案

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

copied from here

just add this property to your Button. you can set your own using the android:stateListAnimator property.

android:stateListAnimator="@null"

full code :

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

UpDate :

for Understanding I set it 10dp..

xml code :

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

这篇关于Android高程未在Button上显示阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆