【问题标题】:CardView cardElevation draws random square inside of CardViewCardView cardElevation 在 CardView 内绘制随机正方形
【发布时间】:2021-08-21 04:18:10
【问题描述】:

一个简单的问题,我使用 CardView 为 Button 添加高度,但是在分配 cardElevation 时,CardView 在自身内部绘制了一个框,我尝试切换高度和其他属性,我可以最小化通过增加cardElevation 来影响它的效果,但这不是解决方案。

这是来自 XML 的 CardView

  <androidx.cardview.widget.CardView
            android:layout_width="38sp"
            android:layout_height="38sp"
            app:cardElevation="5sp"
            android:layout_gravity="end"
            android:layout_marginTop="60sp"
            android:layout_marginRight="12sp"
            android:layout_marginEnd="12sp"
            app:cardBackgroundColor="#97FFFFFF"></androidx.cardview.widget.CardView>

应用程序中的CardView。 (右上角的方框)

整个XML文件,最后一个CardView是图片上的那个。

<?xml version="1.0" encoding="utf-8"?>
<com.flipboard.bottomsheet.BottomSheetLayout
    android:id="@+id/bottomsheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.map.MapFragment">

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

    <androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardElevation="20dp"
        android:layout_gravity="end"
        android:layout_marginTop="60dp"
        android:layout_marginRight="12dp"
        android:layout_marginEnd="12dp"
        app:cardBackgroundColor="#97FFFFFF">

      <ImageButton
          android:layout_width="38dp"
          android:layout_height="38dp"
          android:padding="1dp"
          android:src="@drawable/ic_baseline_refresh_24"
          android:background="#00FFFFFF"
          android:elevation="10dp"
          android:id="@+id/btn_reset_map"/>
    </androidx.cardview.widget.CardView>
        
        <androidx.cardview.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:cardElevation="20dp"
            android:layout_gravity="end"
            android:layout_marginTop="10dp"
            android:layout_marginRight="12dp"
            android:layout_marginEnd="12dp"
            app:cardBackgroundColor="#97FFFFFF">

            <ImageButton
                android:layout_width="38dp"
                android:layout_height="38dp"
                android:padding="1dp"
                android:src="@drawable/ic_baseline_layers_24"
                android:background="#00FFFFFF"
                android:elevation="10dp"
                android:id="@+id/btn_layer_map"/>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="38dp"
            android:layout_height="38dp"
            app:cardElevation="5dp"
            android:layout_gravity="end"
            android:layout_marginTop="40dp"
            android:layout_marginRight="12dp"
            android:layout_marginEnd="12dp"
            app:cardBackgroundColor="#97FFFFFF"></androidx.cardview.widget.CardView>

    </LinearLayout>
</fragment>

</com.flipboard.bottomsheet.BottomSheetLayout>

【问题讨论】:

  • 为什么你的单位都在sp?您是否尝试将它们更改为 dp?
  • 我一直在使用 sp 没有什么特别的原因,现在我读到 sp 用于字体大小,而 dp 用于其他所有内容。现在也用 dp 尝试过,没有任何改变,正方形仍然存在。
  • 我想知道您的 xml 是如何构造的。 cardview的父母是什么?是否可以共享该特定卡片视图所在布局的所有代码?
  • 我在问题末尾添加了xml文件,最后一个CardView是图像上显示的那个。
  • 为什么要使用片段标签作为视图组?

标签: java android android-cardview cardview


【解决方案1】:

您可以通过编程将CardView 背景颜色设置为与app:cardBackgroundColor 设置的颜色相同的颜色

CardView cardview = findViewById(R.id.cardview);
cardview.setBackgroundColor(ContextCompat.getColor(this, R.color.cardview_color));

并在colors.xml中创建这个颜色

<color name="cardview_color">#97FFFFFF</color>

并将这个id添加到xml中:

  <androidx.cardview.widget.CardView
      android:id="@+id/cardview"
      ....

注意:由于某种原因,android:background="#97FFFFFF" 不起作用

【讨论】:

  • CardView 问题中的正方形现在已修复,尽管它删除了 cardCornerRadius,我现在似乎无法添加它。另外,如果我有更多的 CardViews,每个 CardViews 是否应该以相同的方式设置背景颜色?
  • @EdwardAarma 你说得对……让我想想
  • @EdwardAarma 我猜你需要删除app:cardBackgroundColor 颜色的不透明度,这就是你看到内部正方形的原因.. 所以你可以将它设置为app:cardBackgroundColor="#EDEDED" 这将使它不透明..并使用答案中提到的setBackgroundColor取消
猜你喜欢
  • 1970-01-01
  • 2021-08-06
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-26
  • 2016-07-09
  • 1970-01-01
相关资源
最近更新 更多