当前位置: 首页 > 知识库问答 >
问题:

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

微生毅然
2023-03-14

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

<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>

和rounded\u rect\u形状。xml目录下的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>

共有3个答案

梁承恩
2023-03-14

Android中没有这样的属性,显示阴影。但可能的方法是:

>

有一个带阴影的9面片图像,并将其设置为线性布局的背景

居英资
2023-03-14

嗯,这很容易实现。

只需构建一个从黑色变为透明颜色的GradientDrawable,然后使用父关系将形状放置在靠近要有阴影的视图的位置,然后只需为高度或宽度指定任何值。

这是一个例子,这个文件必须在res/draable中创建,我将其命名为shadow.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:startColor="#9444"
        android:endColor="#0000"
        android:type="linear"
        android:angle="90"> <!-- Change this value to have the correct shadow angle, must be multiple from 45 -->
    </gradient>

</shape>

将下面的代码放在上面的LinearLayout中,例如,将android:layout_widthandroid:layout_height设置为fill_parent2.3dp,您将对LinearLayout产生很好的阴影效果。

<View
    android:id="@+id/shadow"
    android:layout_width="fill_parent"
    android:layout_height="2.3dp"  
    android:layout_above="@+id/id_from_your_LinearLayout" 
    android:background="@drawable/shadow">
</View>

注1:如果增加android:layout\u height,则会显示更多阴影。

注2:如果要将此代码放置在RelativeLayout中,请使用上面的android:layout\u=“@id/id\u from\u your\u LinearLayout”属性,否则忽略它。

希望它能帮助别人。

燕禄
2023-03-14

这个问题还有另一个解决方案,实现一个层列表,作为LinearLayoout的背景。

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

<?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
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/background_with_shadow"/>
 类似资料:
  • 我尝试过使用阴影xmls进行线性布局,但似乎没有按照我想要的方式进行。我使用的代码是: 请建议我如何实现这一点。

  • 想改进这个问题吗 通过编辑此帖子,更新问题,使其只关注一个问题。 我想知道是否可以在android中为线性布局创建阴影效果,如下所示。任何帮助都将不胜感激...

  • 本文向大家介绍Android LinearLayout 线性布局,包括了Android LinearLayout 线性布局的使用技巧和注意事项,需要的朋友参考一下 示例 LinearLayout是一种ViewGroup将其子级排列在单列或单行中的。可以通过调用方法setOrientation()或使用xml属性来设置方向android:orientation。 垂直方向:android:orien

  • 在上一节中,我们讲到了所有的 Layout 都是从 ViewGroup 继承而来,它可以包含若干 View 并按照指定的规则将这个 View 摆放到屏幕上。那么接下来的章节我们就来学习一下 Android 的 UI 布局,Android 原生有六大布局,分别是: LinearLayout(线性布局)、RelativeLayout(相对布局)、TableLayout(表格布局)、FrameLayou

  • 主要活动。xml 这是一张圆形的照片。xml: 我尝试了一些阴影效果的代码,但它不起作用。

  • 我的Cardview在Listview中没有显示Android L(Nexus 5)中的阴影。此外,圆边未正确显示。以下是Listview的适配器视图的代码: 以及ListView xml: 它适用于具有适当阴影和圆角的pre-L设备。但在AndroidL设备上无法工作。你能告诉我这里缺少什么吗?