I use a framelayout at the bottom of an activity, in order to show the shadow effects on the fragment, I add android:elevation. But the shadow effects only appear in the bottom side not on top side, any one could give me some advice?
<FrameLayout android:id="@+id/bottom_container" android:background="#00737f" android:layout_width="match_parent" android:layout_height="50dp" android:layout_gravity="bottom" android:elevation="4dp" android:layout_alignParentBottom="true" android:layout_marginBottom="50dp"/>
To set the default (resting) elevation of a view, use the android:elevation attribute in the XML layout. To set the elevation of a view in the code of an activity, use the View. setElevation() method. To set the translation of a view, use the View.
To set elevation shadow only on the bottom on React Native, we wrap out View with another View and set its overflow to 'hidden' . to set overflow to 'hidden' on the outer View . And then we add the shadow styles in the inner view to add the shadow.
TranslationZ is a dynamic property used for animation. Basically it's needed to nicely handle elevation changes. When you press a button, its elevation remains unchanged and its translationZ is being animated.
There is a trick that can be used to display a shadow above a View.
Basically we have to use two nested Layouts, where the outer Layout casts the shadow with an elevation
and the inner layout sets the background
. Then by setting a padding
to the outer Layout, we can shift the inner Layout down, without moving the shadow, thus more of the shadow becomes visible:
<FrameLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_marginBottom="50dp" android:elevation="4dp" android:outlineProvider="bounds" android:paddingTop="2dp" android:layout_marginTop="-2dp"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00737f"> <!-- content --> </FrameLayout> </FrameLayout>
An important thing here is the attribute outlineProvider
, that is required to make the outer Layout cast a shadow even without having a background set.
Further we specify a negative margin
to compensate for the offset created by the padding. Depending on the use-case we can omit that.
But attention: If we shift the View too much, some rendering artifacts become visible:
Source of this example on Github
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With