Ich habe dieses minimale Android Studio-Projekt mit nur einem Knopf. ich die Taste einen Schatten zuweisen mit:Wie kann ich Tastenerhöhungen (Schatten) in Android APIs unter 21 emulieren?
android:elevation="3dp"
android:translationZ="3dp"
android:stateListAnimator="@null"
und ich sehe die Schatten in Android Studio Design-Register. Aber ich habe auch eine Warnung in dem XML-Editor
atribute ... nur in API-Ebene verwendet wird 21 und höher (Strom min 16)
Und in der Tat wird Schaltfläche Schatten angezeigt nur in Emulatoren mit API level 21 or higher
, und es wird flach gezeigt, ohne Schatten in Emulatoren mit API level lower than 21
.
Die konkrete Frage ist also, wie kann ich den Schatteneffekt in API's lower than 21
emulieren.
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>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Knopf zeigt Schatten, in API 22
:
Gleiche Taste nicht angezeigt Schatten, in API 16
:
@Ramiro hast du es oder nicht ??? – Ironman
Ja @Ironman, es funktioniert. Aber es erfordert eine Menge Arbeit, die sich mit gedrückten, fokussierten usw. Tasten beschäftigt. Also lasse ich das Erscheinungsbild der Schaltfläche so, wie es von Google in jeder API beabsichtigt war. Vielen Dank! – Ramiro