简体   繁体   中英

Android - spotShadowAlpha only for linearlayout

I am using the elevation attribute for my linearlayout, but the shadow is to bright. I need a darker shadow only for the linear layout.

I added android:spotShadowAlpha to my styles.xml . It worked, but not only for the linear layout. Every View has a darker shadow.

Styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:ambientShadowAlpha">0</item>
        <item name="android:spotShadowAlpha">0.55   </item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

linearlayout in activity_main.xml

<LinearLayout
        android:id="@+id/linl"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_marginStart="55dp"
        android:layout_marginTop="208dp"
        android:layout_marginEnd="56dp"
        android:layout_marginBottom="209dp"
        android:background="@drawable/custom_buttom"
        android:elevation="70dp"
        android:orientation="horizontal"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
</LinearLayout>

Background @drawable/custom_buttom

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#FFFFFF"/>
    <corners android:radius="999dp"/>
</shape>

DISCLAIMER: ONLY WORKS CORRECTLY FOR API 28 AND ABOVE

I know this question is kind of old, but i ran into the same problem. This guy has a pretty good solution. Essentially you set android:ambientShadowAlpha and android:spotShadowAlpha to 1 . then you can use SetOutlineAmbientShadowColor and SetOutlineSpotShadowColor using a value that contains an alpha value to set it on an item by item basis. For everything else these act as the default shadow colors with system default alpha values:

<color name="defaultOutlineAmbientShadowColor">#0A000000</color> <color name="defaultOutlineSpotShadowColor">#30000000</color>

These are set in styles.xml:

<item name="android:ambientShadowAlpha">1</item> <item name="android:outlineAmbientShadowColor">@color/defaultOutlineAmbientShadowColor</item> <item name="android:spotShadowAlpha">1</item> <item name="android:outlineSpotShadowColor">@color/defaultOutlineSpotShadowColor</item>

android:ambientShadowAlpha and android:spotShadowAlpha theme attributes values are used at pretty low level, so there is no way to set specific values to these attributes for particular view of hierarchy (even using theme overlay approach ). It is possible to set custom values statically per activity basis:

  1. create custom theme derived from your app theme and override android:ambientShadowAlpha and android:spotShadowAlpha in it:

     <style name="Theme.YourApp" parent="..."> ... </style> <style name="Theme.YourApp.StrongShadows"> <item name="android:ambientShadowAlpha">1</item> <item name="android:spotShadowAlpha">1</item> </style>
  2. In manifest set this theme for your activity:

     <application android:theme="@style/Theme.YourApp"> <activity android:name=".ActivityWithDefaultShadows" /> <activity android:name=".ActivityWithStrongShadows" android:theme="@style/Theme.YourApp.StrongShadows" /> </application>

The best effort you can make at the moment to have better shadow customization is the approach that @ottermatic suggested in his answer ( which of course works only for API level 28 and above ). I'll describe it more accuratly:

  1. in themes.xml file in values resources folder define base app theme and app theme derived from based without mentioned attributes:

     <style name="Base.Theme.YourApp" parent="..."> ... </style> <style name="Theme.YourApp" parent="Base.Theme.YourApp"/>
  2. in themes.xml file in values-v28 override app theme for api versions 28 and above:

     <style name="Theme.YourApp" parent="Base.Theme.YourApp"> <item name="android:ambientShadowAlpha">1</item> <item name="android:spotShadowAlpha">1</item> <item name="android:outlineAmbientShadowColor">@color/defaultOutlineAmbientShadowColor</item> <item name="android:outlineSpotShadowColor">@color/defaultOutlineSpotShadowColor</item> </style>

We set android:ambientShadowAlpha and android:spotShadowAlpha to 1 so they do not influence the shadow color. And instead we can customize shadow by changing android:outlineAmbientShadowColor and android:outlineSpotShadowColor attributes - they have reasonable default values (for default shadows to be similar to shadows on previous api versions) set in app theme but also their values can be overriden for each separate view:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:text="Button with default theme shadow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button2"
        android:outlineAmbientShadowColor="#55FF0000"
        android:outlineSpotShadowColor="#55FF0000"
        android:text="Button with custom red shadow"
        app:layout_constraintTop_toBottomOf="@id/button1"
        app:layout_constraintStart_toEndOf="@id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</androidx.constraintlayout.widget.ConstraintLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM