如何为线性布局显示阴影。我想要白色圆形背景和线性布局周围的阴影。到目前为止我已经这样做了。请帮我。提前谢谢。
<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>
答案 0 :(得分:142)
通过实现一个将作为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>
然后在LINELayout中添加图层列表作为背景。
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_with_shadow"/>
答案 1 :(得分:28)
嗯,这很容易实现。
只需构建一个来自黑色的GradientDrawable
并转换为透明色,而不是使用父关系将形状放置在想要有阴影的视图附近,那么您只需要提供任何值高度或宽度。
以下是一个示例,此文件必须在res/drawable
中创建,我将其命名为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_width
和android:layout_height
设置为fill_parent
和2.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_height
,则会显示更多阴影。
注2:如果您将此代码放在RelativeLayout中,请使用android:layout_above="@+id/id_from_your_LinearLayout"
属性,否则请忽略它。
希望对某人有所帮助。
答案 2 :(得分:25)
在Android中没有这样的属性来显示阴影。但可能的方法是:
添加一个灰色的普通LinearLayout,在其上添加您的实际布局,底部和右侧的边距等于1或2 dp
使用带阴影的9补丁图像并将其设置为线性布局的背景
答案 3 :(得分:17)
对于棒棒糖及以上,您可以使用elevation。
对于旧版本:
这是一个懒惰的黑客来自: http://odedhb.blogspot.com/2013/05/android-layout-shadow-without-9-patch.html
(toast_frame在KitKat上不起作用,阴影从吐司中移除)
只需使用:
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>
答案 4 :(得分:10)
试试这个.. 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"
答案 5 :(得分:5)
实际上我同意@odedbreiner,但是我将dialog_frame放在第一层内,并隐藏了白色层下的黑色背景。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@android:drawable/dialog_frame"
android:right="2dp" android:left="2dp" android:bottom="2dp" android:top="5dp" >
<shape android:shape="rectangle">
<corners android:radius="5dp"/>
</shape>
</item>
<item>
<shape
android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:radius="5dp"/>
</shape>
</item>
</layer-list>
答案 6 :(得分:3)
9.png
)
2.将其保存在drawable
。
3.将其设置为您的布局。
4.set padding。
例如:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/shadow"
android:paddingBottom="6dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="6dp"
>
.
.
.
</LinearLayout>
答案 7 :(得分:2)
通过名为&#34; shadow.xml&#34;的示例创建一个新的XML;在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="@color/middle_grey"/>
</shape>
</item>
<item android:left="2dp"
android:right="2dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="@color/white"/>
</shape>
</item>
</layer-list>
在LinearLayout或您想要创建阴影的其他窗口小部件中创建XML后,可以使用BACKGROUND属性查看效果。它会是这样的:
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingRight="@dimen/margin_med"
android:background="@drawable/shadow"
android:minHeight="?attr/actionBarSize"
android:gravity="center_vertical">
答案 8 :(得分:2)
您可以将以下类用于xml标记:
<yourpackagename.ShadowLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
app:sl_shadow_color="#9e000000"
app:sl_shadow_radius="4dp">
<child views>
</yourpackagename.ShadowLayout>
在xml中使用Tag,如下所示:
<declare-styleable name="ShadowLayout">
<attr name="sl_shadowed" format="boolean"/>
<attr name="sl_shadow_distance" format="dimension"/>
<attr name="sl_shadow_angle" format="integer"/>
<attr name="sl_shadow_radius" format="dimension"/>
<attr name="sl_shadow_color" format="color"/>
</declare-styleable>
<强>更新强>
将以下代码放在资源&gt;&gt;值
中的attrs.xml中DatabaseReference mref=databaseMessages.child(chattingUser.getUserId()+", "+firebaseAuth.getCurrentUser().getUid());
mref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
答案 9 :(得分:2)
我知道这很旧,但是大多数答案都需要大量额外的代码。
如果您的背景色浅,则可以使用以下方法:
android:elevation="25dp"
答案 10 :(得分:1)
一种可能的解决方案是使用九个补丁图像,如http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
OR
我是通过以下方式完成的。这是我的主要布局,其中round_corner.xml和drop_shadow.xml用作后台资源。 round_corner_two与round_corner.xml相同,只是颜色属性不同。将round_corner.xml,drop_shadow.xml和round_conere_two.xml复制到drawable文件夹中。
<RelativeLayout
android:id="@+id/facebook_id"
android:layout_width="250dp"
android:layout_height="52dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:background="@drawable/round_corner" >
<LinearLayout
android:id="@+id/shadow_id"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="1dp"
android:background="@drawable/drop_shadow" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginBottom="2dp"
android:background="@drawable/round_corner_two"
android:gravity="center"
android:text="@string/fb_butn_text"
android:textColor="@color/white" >
</TextView>
</LinearLayout>
</RelativeLayout>
round_corner.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- view background color -->
<solid
android:color="#ffffff" >
</solid>
<!-- view border color and width -->
<stroke
android:width="0dp"
android:color="#3b5998" >
</stroke>
<!-- If you want to add some padding -->
<padding
android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" >
</padding>
<!-- Here is the corner radius -->
<corners
android:radius="10dp" >
</corners>
</shape>
drop_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="@android:color/darker_gray" />
<corners android:radius="12dp"/>
</shape>
</item>
<item android:right="1dp" android:left="1dp" android:bottom="5dp">
<shape
android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:radius="5dp"/>
</shape>
</item>
</layer-list>
答案 11 :(得分:1)
我知道这太迟了。但我有同样的要求。我这样解决了
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardUseCompatPadding="true"
app:cardElevation="4dp"
app:cardCornerRadius="3dp" >
<!-- put whatever you want -->
</android.support.v7.widget.CardView>
您需要添加依赖项:
compile 'com.android.support:cardview-v7:25.0.1'
答案 12 :(得分:0)
将此xml drawable设置为您的背景; ---
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle" >
<solid android:color="#d8d8d8" />-->Your shadow color<--
<corners android:radius="15dp" />
</shape>
</item>
<!-- White Top color -->
<item android:bottom="3px" android:left="3px" android:right="3px" android:top="3px">-->here you can customize the shadow size<---
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<corners android:radius="15dp" />
</shape>
</item>
</layer-list>