【问题标题】:Linear layout cutting material cards shadow线性布局切割材料卡阴影
【发布时间】:2018-08-02 20:18:38
【问题描述】:

我正在制作一个带有材质卡及其阴影的天气应用程序。 我找到了一种使用线性布局将它们居中的方法,但它会切断阴影。 我怎样才能防止这种情况?有没有办法在不使用线性布局或框架布局的情况下对齐它们?

这是我的布局代码: 我使用 FrameLayout 作为 root,我的 LinearLayout 包含两个材质卡,我只是希望它们作为一个组居中,如果您知道另一种方法,请告诉我!

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
    android:id="@+id/img_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/background"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:fontFamily="cursive"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="25dp"
    android:textSize="50sp"
    android:textColor="#FFF"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Your location's weather in a touch!"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="90dp"
    android:textSize="17.3sp"
    android:textColor="@color/colorPrimaryText"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center">
    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="270dp"
        android:layout_height="140dp"
        android:layout_gravity="center_horizontal"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="5dp">

        <TextView
            android:id="@+id/tv_temperature"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:padding="16dp"
            android:text="30°C"
            android:textColor="@color/colorPrimaryText"
            android:textSize="50sp" />
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cv_data"
        android:layout_width="270dp"
        android:layout_height="140dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="5dp">

        <TextView
            android:id="@+id/tv_conditions"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:padding="16dp"
            android:text="Scatted Clouds"
            android:textColor="@color/colorPrimaryText"
            android:textSize="30sp" />
    </android.support.v7.widget.CardView>
</LinearLayout>

<ProgressBar
    android:id="@+id/pb_loading"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:layout_gravity="center"
    android:elevation="20dp"/>
<TextView
    android:id="@+id/tv_city_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:gravity="center"
    android:padding="20dp"
    android:fontFamily="sans-serif"
    android:textColor="#FFF"
    android:textSize="20sp"/>

【问题讨论】:

  • 你的&lt;LinearLayout&gt; 里面是什么?如果我们能看到整个布局会更容易回答。
  • 更新了整个布局文件。
  • 从你发的图片可以看出,蓝色背景隐藏了阴影

标签: android xml android-studio layout material-design


【解决方案1】:

看起来您的布局使用 ConstraintLayout 而不是 FrameLayout 会更好,但我们仍然可以使您拥有的内容发挥作用。

问题在于(在较新的 Android 版本上),CardView 的阴影实际上是在视图边界之外绘制的。通常这很好,但如果 CardView 在父级内部,则如果父级没有足够的空间显示阴影,则该父级可以剪切阴影。

在您的情况下,“简单修复”实际上非常简单。将LinearLayout 的宽度更改为match_parent;这将为卡片提供绘制阴影的空间。

编辑

我上面所说的将解决每张卡片侧面的阴影,但不会解决顶部卡片上方或底部卡片下方的阴影。同样,本着快速修复的精神,我建议将这些属性添加到您的 LinearLayout:

android:paddingTop="8dp"
android:paddingBottom="8dp"
android:clipToPadding="false"

【讨论】:

  • 谢谢!这是我的第一个应用程序,我制作它只是为了学习一些 android studio,所以允许简单的修复! :D
【解决方案2】:

您需要更改height并将layout_gravity更改为gravity,如下面的代码:

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

它可能会解决你的问题 =]

如果你想改进你的代码,我建议你不要设置固定的宽度和高度,它会给你带来一些设备上的问题。你也可以只加载一次xmlns:card_view,在你的LinearLayout,这是你的第一个标签

【讨论】:

  • 谢谢,它们已经对齐,但主要问题是阴影。如果我从第二个 card_view 中删除 xmlns:card_view,它的属性会变成红色¯_(•_• )_/¯
  • Android 从 CardView 中创建这个阴影。使用他的父 ViewGroup 作为wrap_content 它可能不会显示整个阴影。在这种情况下,您可以在父 ViewGroup 中使用 match_parent。抱歉,我忘了说将xmlns:card_view 添加到LinearLayout 中,这是您在其布局中的第一个标签。通过这样做,您可以在他的任何孩子中使用card_view:.. =]
猜你喜欢
  • 1970-01-01
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
  • 2021-09-27
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 2015-12-01
相关资源
最近更新 更多