Android XML: android:elevation vs. app:elevation

2020-02-10 00:43发布

问题:

When do I use android:elevation and when app:elevation?

What's the difference between those two?

回答1:

Hope I can help, Let's talk with an example:

<android.support.design.widget.FloatingActionButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    ...
    android:elevation="@dimen/elevation_medium"
    /> 

The android:elevationattribute will work from the API level 21 and upper.

<android.support.design.widget.FloatingActionButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    ...
    app:elevation="@dimen/elevation_medium"
    /> 

In this case the app:elevation attribute belongs to the FloatingActionButton styleable, inside de Android Design Support Library which will work from version 4 of the framework, instead version 21, the case of android:elevation.



回答2:

app:elevation - if you are using support library (for supporting old versions of Android) android:elevation - if you are not using support library



回答3:

There is one more thing:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/spacing"
    android:src="@drawable/svg_my_location"
    app:elevation="4dp" />

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/bottomSheet"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@drawable/shape_rect_round_white"
    app:elevation="8dp"
    app:behavior_hideable="true"
    app:behavior_peekHeight="100dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"/>

With using app FloatingActionButton is visible

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/spacing"
    android:src="@drawable/svg_my_location"
    android:background="4dp" />

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/bottomSheet"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@drawable/shape_rect_round_white"
    android:background="8dp"
    app:behavior_hideable="true"
    app:behavior_peekHeight="100dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"/>

When you're using android FloatingActionButton is invisible, because it overlapped by bottom sheet