首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android的线性布局中显示阴影?

如何在Android的线性布局中显示阴影?
EN

Stack Overflow用户
提问于 2012-10-22 14:01:17
回答 9查看 180.7K关注 0票数 79

如何为我的线性布局显示阴影。我想要白色的圆形背景周围的线性布局阴影。到目前为止我已经做到了这一点。

<LinearLayout
 android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@xml/rounded_rect_shape"
android:orientation="vertical"
android:padding="10dp">
<-- My buttons, textviews, Imageviews go here -->
</LinearLayout>

和xml目录下的rounded_rect_shape.xml

 <?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:bottomLeftRadius="3dp"
      android:bottomRightRadius="3dp"
      android:topLeftRadius="3dp"
      android:topRightRadius="3dp" />
</shape>
EN

回答 9

Stack Overflow用户

发布于 2013-02-14 17:06:41

对于这个问题,还有另一种解决方案,那就是实现一个层列表,它将作为LinearLayoout的背景。

将background_with_shadow.xml文件添加到res/drawable。包含:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <shape 
            android:shape="rectangle">
        <solid android:color="@android:color/darker_gray" />
        <corners android:radius="5dp"/>
        </shape>
    </item>
    <item android:right="1dp" android:left="1dp" android:bottom="2dp">
        <shape 
            android:shape="rectangle">
        <solid android:color="@android:color/white"/>
        <corners android:radius="5dp"/>
        </shape>
    </item>
</layer-list>

然后在你的LinearLayout中添加图层列表作为背景。

<LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/background_with_shadow"/>
票数 146
EN

Stack Overflow用户

发布于 2013-05-30 20:58:55

对于棒棒糖和更高版本,你可以使用elevation

对于较旧的版本:

这是来自:http://odedhb.blogspot.com/2013/05/android-layout-shadow-without-9-patch.html的一个懒惰的技巧

(toast_frame在KitKat上不起作用,阴影已从toasts中删除)

只需使用:

android:background="@android:drawable/toast_frame"

或者:

android:background="@android:drawable/dialog_frame"

作为背景

示例:

<TextView
        android:layout_width="fill_parent"
        android:text="I am a simple textview with a shadow"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:padding="16dp"
        android:textColor="#fff"
        android:background="@android:drawable/toast_frame"
        />

和不同的bg颜色:

<LinearLayout
        android:layout_height="64dp"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:background="@android:drawable/toast_frame"
        android:padding="4dp"
        >
    <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Button shadow"
            android:background="#33b5e5"
            android:textSize="24sp"
            android:textStyle="bold"
            android:textColor="#fff"
            android:layout_gravity="center|bottom"
            />

</LinearLayout>
票数 18
EN

Stack Overflow用户

发布于 2016-03-10 18:39:36

试试这个..。layout_shadow.xml

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

    <item
        android:left="0dp"
        android:right="0dp"
        android:top="0dp"
        android:bottom="2dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <corners android:radius="2dp" />
        </shape>
    </item>
</layer-list>

应用于您的布局,如下所示

 android:background="@drawable/layout_shadow"
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13005714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档