【问题标题】:Elevation doesn't work on LinearLayout高程不适用于 LinearLayout
【发布时间】:2018-03-15 23:12:27
【问题描述】:

我正在为我的 Android 应用程序构建自定义布局,我正在尝试向一个 LinearLayout 添加高度,但它不起作用,我尝试了很多在网上找到的解决方案,但都没有成功。

我的 xml 是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">

<de.hdodenhof.circleimageview.CircleImageView
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:src="@drawable/admin_gray"
    app:civ_border_width="2dp"
    app:civ_border_color="@android:color/darker_gray"
    android:layout_marginRight="10dp"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvNewsAggiuntoCommentoDescrizione"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Username ha commentato una ricetta:"/>

        <TextView
            android:id="@+id/tvNewsAggiuntoCommentoData"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="25/05/1989 12:58:07"
            android:textColor="@android:color/darker_gray" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/white"
        android:elevation="10dp">

        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rounded_linear_layout_tile_ricetta"
            android:layout_marginBottom="5dp"
            >

            <ImageView
                android:id="@+id/imageViewRicetta"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:src="@mipmap/cibo"
                android:scaleType="centerCrop"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Antipasto"
                android:background="@color/colorCategoriaRicetta"
                android:layout_alignBaseline="@id/imageViewRicetta"
                android:layout_gravity="right|top"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white"
                android:padding="3dp"
                android:id="@+id/tvCategoriaRicetta"
                android:elevation="4dp"/>

            <TextView
                android:id="@+id/tvTitoloRicetta"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Maxi hamburger con maionese"
                android:background="@color/colorBackgroundTextViewNomeRicetta"
                android:layout_alignBaseline="@id/imageViewRicetta"
                android:layout_gravity="left|bottom"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@android:color/white"
                android:paddingLeft="5dp"
                android:textStyle="bold" />

        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/imageViewFotoAltroUtente"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:src="@drawable/admin_gray"
                app:civ_border_width="2dp"
                app:civ_border_color="@android:color/darker_gray" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="5dp">

                <TextView
                    android:id="@+id/tvUsernameUtenteCommento"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:text="Username"/>

                <TextView
                    android:id="@+id/tvNewsAggiuntoCommentoCommento"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Questo è il testo di esempio di un commento a una ricetta"/>

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>


</LinearLayout>

注意,带有elev​​ation的布局不是顶部LinearLayout,而是带有“elevation”属性的布局...

您能帮我找到一个解决方案以正确获取海拔高度吗?

非常感谢。

【问题讨论】:

  • 您能否提供一个示例屏幕截图模型,说明您希望结果是什么样的?
  • 使用海拔的最佳方式是在卡片视图中。不然就太bug了。

标签: android xml layout android-linearlayout elevation


【解决方案1】:

把这个礼节加给你LinearLayout

android:outlineProvider="bounds"

确保您要设置高程的元素的所有视图父级都没有填充。这会导致阴影被剪裁。一个解决方案是将此属性添加到所有父视图:

android:clipToPadding="false"

此外,如果清单中有此行,阴影将不会显示:

android:hardwareAccelerated="false"

【讨论】:

  • 嗨@GuilhermeFGL,感谢您的回答,我已在 LL 上插入您的行,但清单中没有该行,无论如何它不起作用...
  • 您用于测试视图的 API 版本是什么?
  • API 24 (Android 7.0)
  • 请注意,Android Studio 的 Design 预览中不会显示海拔,尽管它会在模拟器和物理设备上显示。
  • 我更新了我的答案。请尝试一下,并注意@KalaBalik 的评论
猜你喜欢
  • 1970-01-01
  • 2021-05-26
  • 2016-12-02
  • 1970-01-01
  • 2012-09-27
  • 2014-01-17
  • 2015-08-24
  • 1970-01-01
  • 2019-04-17
相关资源
最近更新 更多