如何在Android视图上方添加阴影 [英] How do add shadow above a view in android

查看:274
本文介绍了如何在Android视图上方添加阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个作为页脚标题的服务器视图.这只是一种视图,您可能会认为它是按钮,文本视图或布局(我确实对任何事物都开放).这是xml

I have a view that is to server as a footer title. It is just a view, that you may think of as a button or a textview or a layout (I am open to anything really). Here is the xml

<RelativeLayout>
   <ScrollView >… </ScrollView> //match parent width and height
   <MyBottomView/> // align parent bottom
 </RelativeLayout>

因此您可以看到ScrollView确实在MyBottomView下方滚动.我想在MyBottomView中添加一个顶部阴影,使它看起来更像Material Design.我该怎么办?

So as you can see the ScrollView does scroll below MyBottomView. I want to add a top shadow to the MyBottomView so it looks more like Material Design. How might I do that?

推荐答案

以下是针对此问题的一些解决方案-尽力而为:

Here are some solutions for this problem - choose your best:

  • On StackOverFlow, in post from 22th October 2012, How to show shadow around the linearlayout in android?, you would read:

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

There is no such attribute in Android, to show a shadow. But possible ways to do it are:

  1. 添加带有灰色的普通LinearLayout,在其上添加您的实际布局,底部和右侧的边距等于1或2 dp

  1. Add a plain LinearLayout with grey color, over which add your actual layout, with margin at bottom and right equal to 1 or 2 dp

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

Have a 9-patch image with a shadow and set it as the background to your Linear layout

解决该问题的另一种方法是实现一个层列表,该列表将用作LinearLayoout的背景.

There is also another solution to the problem by implementing a layer-list that will act as the background for the LinearLayoout.

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

Add background_with_shadow.xml file to res/drawable. Containing:

<?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中的背景.

Then add the the layer-list as background in your LinearLayout.

<LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/background_with_shadow"/>

您还可以阅读: http: //odedhb.blogspot.com/2013/05/android-layout-shadow-without-9-patch.html

  • Another post from StackOverflow, how to set shadow to a View in android?, gives you another solution (using two views that form the shadow.):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:padding="10dp"
    android:background="#CC55CC">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="0">
            <TableRow>
                <LinearLayout
                    android:id="@+id/content"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <TextView  
                        android:layout_width="fill_parent" 
                        android:layout_height="wrap_content"
                        android:background="#FFFFFF" 
                        android:text="@string/hello" />
                </LinearLayout>
                <View
                    android:layout_width="5dp"
                    android:layout_height="fill_parent"
                    android:layout_marginTop="5dp"
                    android:background="#55000000"/>
            </TableRow>
        </TableLayout>
        <View
            android:layout_width="fill_parent"
            android:layout_height="5dp"
            android:layout_marginLeft="5dp"
            android:background="#55000000"/>
    </LinearLayout>
</FrameLayout>

  • 您还可以使用特定的可绘制表单Android资源来模拟阴影效果.查看:thttps://stackoverflow.com/questions/21211870/android-view-shadow或仅阅读以下文章:
  • 我正在使用Android Studio 0.8.6,却找不到:

    I'm using Android Studio 0.8.6 and I couldn't find:

    android:background="@drawable/abc_menu_dropdown_panel_holo_light"
    

    所以我找到了它:

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

    它看起来像这样:

    ![在此处输入图片描述] [1]

    ![enter image description here][1]

    [1]: http://i.stack.imgur.com/tId9L.jpg


    如果您对干净的Material Design效果不感兴趣,请阅读以下文档:


    If you're insteresting in clean Material Design effect, read some documentation like below:

    这篇关于如何在Android视图上方添加阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆