【问题标题】:elevation not working in LinearLayout海拔在 LinearLayout 中不起作用
【发布时间】:2019-01-03 07:21:03
【问题描述】:

bg_book_promotion 是需要有阴影的图片 当我添加android:elevation 时,它不起作用。 哪位能告诉我怎么改,好奇怪。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/color_fff_white"
    android:orientation="vertical">

    <LinearLayout
            android:id="@+id/ll_promotion_bg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/margin9"
            android:layout_marginLeft="@dimen/margin7"
            android:layout_marginRight="@dimen/margin7"
            android:layout_marginTop="@dimen/margin14"
            android:background="@drawable/bg_book_promotion"
            android:elevation="@dimen/height2"
            android:gravity="center_vertical"
            android:orientation="vertical"
            android:paddingLeft="@dimen/padding9">

            <TextView
                android:id="@+id/tv_promotion_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/color_fff_white"
                android:textSize="@dimen/textsize15"
                android:textStyle="bold"
                tools:text="" />

            <TextView
                android:id="@+id/tv_promotion_desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:textColor="@color/color_fff_white"
                android:textSize="@dimen/textsize12"
                tools:text="" />
        </LinearLayout>
 </LinearLayout>

【问题讨论】:

  • 可以加截图吗?
  • android:elevation 适用于棒棒糖及以上安卓操作系统。
  • 设备在棒棒糖上方
  • 最后附上输出截图。

标签: android


【解决方案1】:

只需添加 android:outlineProvider="bounds"

<LinearLayout
    android:id="@+id/ll_promotion_bg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/margin9"
    android:layout_marginLeft="@dimen/margin7"
    android:layout_marginRight="@dimen/margin7"
    android:layout_marginTop="@dimen/margin14"
    android:background="@drawable/bg_book_promotion"
    android:elevation="@dimen/height2"
    android:outlineProvider="bounds"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:paddingLeft="@dimen/padding9">

【讨论】:

    【解决方案2】:

    只需使用 CardView 作为线性布局的父级,您将获得提升。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_fff_white"
        android:orientation="vertical">
    
    <android.support.v7.widget.CardView 
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="8dp"
        card_view:cardCornerRadius="3dp">
    
    <LinearLayout
                android:id="@+id/ll_promotion_bg"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/margin9"
                android:layout_marginLeft="@dimen/margin7"
                android:layout_marginRight="@dimen/margin7"
                android:layout_marginTop="@dimen/margin14"
                android:background="@drawable/bg_book_promotion"
                android:elevation="@dimen/height2"
                android:gravity="center_vertical"
                android:orientation="vertical"
                android:paddingLeft="@dimen/padding9">
    
                <TextView
                    android:id="@+id/tv_promotion_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/color_fff_white"
                    android:textSize="@dimen/textsize15"
                    android:textStyle="bold"
                    tools:text="" />
    
                <TextView
                    android:id="@+id/tv_promotion_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:textColor="@color/color_fff_white"
                    android:textSize="@dimen/textsize12"
                    tools:text="" />
            </LinearLayout>
    
    </android.support.v7.widget.CardView>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2021-05-05
      • 2018-03-05
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 2016-05-22
      • 2023-03-17
      • 1970-01-01
      • 2015-08-18
      相关资源
      最近更新 更多