【问题标题】:I'm facing a problem with CardView, and Material Design elevation, when view's background is changed当视图的背景发生变化时,我遇到了 CardView 和 Material Design 提升的问题
【发布时间】:2020-06-16 14:18:05
【问题描述】:

当我试图改变CardView的背景,或者它是覆盖CardView整个区域的孩子时, 高程阴影消失。

但是,当我使用app:contentPadding 属性时,海拔 阴影再次变得可见,但现在我在CardView 上有了这个奇怪的填充,这当然不是很好看。我发现,Material Design 的观点也存在这样的问题。 这是一个没有背景的MaterialToolbar,这是with 背景。下面是我的 XML 代码。

   <?xml version="1.0" encoding="utf-8"?>

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:cardCornerRadius="8dp"
    app:cardUseCompatPadding="true"
    app:cardElevation="4dp"
    >

        <androidx.appcompat.widget.LinearLayoutCompat
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:background="@android:color/holo_green_dark"
            >
            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/news_image"
                android:layout_width="match_parent"
                android:layout_height="232dp"
                />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/txt_news_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/numans"
                android:textSize="18sp"
                android:textStyle="bold"
                android:layout_margin="12dp"
                android:textColor="@color/colorToolBarDarkSeaBlue"
                />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/txt_news_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-light"
                android:layout_margin="12dp"
                />

        </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.cardview.widget.CardView> 

这是 MaterialToolbar 的代码

  <?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <com.google.android.material.appbar.MaterialToolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_margin="8dp"
        android:elevation="8dp"
        />

</androidx.appcompat.widget.LinearLayoutCompat>

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

    创建一个圆角半径为卡片内半径的drawable,将其设置为cardview内布局的背景。然后您可以调整布局的边距或背景圆角半径以调整阴影视图。

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    
    <solid android:color="#2e8b57" />
    
    
    <corners android:radius="5dp"/>
    
    
    
    
    </shape>
    

    【讨论】:

    • 成功了!你刚刚让我成为了一天的男人。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2015-02-06
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 2021-11-18
    相关资源
    最近更新 更多