Android XML: android:elevation vs. app:elevation

南楼画角 提交于 2019-11-27 17:26:35

问题


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



来源:https://stackoverflow.com/questions/33054316/android-xml-androidelevation-vs-appelevation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!