我只用一个按钮就可以实现这个最小的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) 我有以下方法:
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()'的结果
我怎样才能重写上面的代码片段来摆脱那个警告?
谢谢!
将我的代码删除到最小的例子:
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设计器中可以看到阴影:
但是在运行时没有显示:
测试: