android 在视图下方添加阴影

android add shadow below view

我只是尝试向我的 LinearLayout 添加阴影,使我的视图像这张图片一样在其他视图上运行 facebook messenger

我尝试将此 xml 作为我视图的背景,但它看起来像一条线而不是阴影

<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="@color/black_alpha_12" 
android:endColor="@android:color/transparent" android:angle="90.0" />
</shape>

我在 Whosebug 中尝试了很多 xml 代码,但没有人帮助我制作我想要的阴影

试试这个

getSupportActionBar().setElevation(10);

使用

getSupportActionBar().setElevation(10);

But as per Your Above Comment in my app i made an LinearLayout as ActionBar did this will work

比使用

YourView.setElevation(10);

试试这个

getSupportActionBar().setElevation(10);

另一个我在这里回答的参考资料

希望对您有所帮助

阴影效果HI你可以cardview或者view.setElevation(10);两者都可以。

ref-

ref-https://material.io/guidelines/material-design/elevation-shadows.html#

使用<layer-list>设置视图背景。 像这样

<TextView
  android:id="@+id/tv_item"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/keyboard_btn_bg"
    />

keyboard_btn_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <size
                android:width="130dp"
                android:height="25dp" />
            <corners android:radius="8dp" />

            <padding
                android:bottom="2dp"/>
            <stroke
                android:width="0dp"
                android:color="#20000000" />
            <solid android:color="#20000000" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <size
                android:width="160dp"
                android:height="25dp" />
            <corners android:radius="8dp" />
            <padding
                android:top="8dp"
                android:bottom="8dp"
                android:left="8dp"
                android:right="8dp"
                />
            <stroke
                android:width="1dp"
                android:color="#808080" />
            <solid android:color="#ffffff" />
        </shape>
    </item>

属性提升仅用于 API 21 级及更高级别 例如,一些标签也可以不使用,例如 API 21.

中引入的布局中的新元素

在 xml 中设置高度 属性 为 10dp 或 8dp android:elevation="10dp"