小编Ram*_*iro的帖子

如何在Android API中模拟低于21的按钮高程(阴影)?

我只用一个按钮就可以实现这个最小的Android Studio项目.我为按钮指定了一个阴影:

android:elevation="3dp"
android:translationZ="3dp"
android:stateListAnimator="@null"
Run Code Online (Sandbox Code Playgroud)

我在Android Studio Design标签中看到了阴影.但我也在xml编辑器中收到警告

属性...仅用于API级别21及更高级别(当前最小值为16)

实际上,按钮阴影仅在模拟器中显示API level 21 or higher,并且显示为平面,在模拟器中没有阴影API level lower than 21.

所以具体问题是,如何模拟阴影效果API's lower than 21.

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"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:id="@+id/main_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:weightSum="1">

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

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

        </RelativeLayout>
    </LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

} …
Run Code Online (Sandbox Code Playgroud)

android android-studio material-design android-elevation

8
推荐指数
2
解决办法
8951
查看次数

如何摆脱Android Studio警告"没有抛出getException()的结果"?

我有以下方法:

private void recoverPassword() {

    FirebaseAuth mAuth = FirebaseAuth.getInstance();        

    mAuth.sendPasswordResetEmail("mail@example.com").addOnCompleteListener(new OnCompleteListener<Void>() {

        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (!task.isSuccessful()) {
                Exception e = task.getException();
                System.out.println(e.toString());
        }
    });

}
Run Code Online (Sandbox Code Playgroud)

我一直收到Android Studio警告:

不抛出'getException()'的结果

我怎样才能重写上面的代码片段来摆脱那个警告?

谢谢!

android firebase android-studio firebase-authentication

8
推荐指数
1
解决办法
1026
查看次数

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

    <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>
Run Code Online (Sandbox Code Playgroud)

我需要那种布局结构.

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

}
Run Code Online (Sandbox Code Playgroud)

在Android Studio设计器中可以看到阴影:

在此输入图像描述

但是在运行时没有显示:

在此输入图像描述

测试:

  • Genymotion谷歌Nexus 5X - Marshmallow 6.0.0 - API 23
  • Genymotion谷歌Nexus 5 - 棒棒糖5.1.0 - API 22
  • Genymotion三星Galaxy S2 - Jelly Bean 4.1.1 - API 16
  • 硬件设备,三星Galaxy …

java android button android-studio material-design

4
推荐指数
1
解决办法
2万
查看次数