java - CardView cardElevation 在 CardView 内部绘制随机方 block

标签 java android android-cardview cardview

一个简单的问题,我使用 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。 (右上角的方框)

enter image description here

整个 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>

最佳答案

您可以通过编程方式将 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" 不工作

关于java - CardView cardElevation 在 CardView 内部绘制随机方 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67814722/

相关文章:

java - Spring Data JPA Projection 从数据库中选择的字段

android - AlertDialog 自定义标题有黑色边框

java - Android:将卡片 View 从一个 fragment 发送到另一个 fragment

java - 访问aspectj中的私有(private)字段

java - 自定义 Volley 请求上的 NullPointerException

java - DataOutputStream#writeBytes(String) 与 BufferedWriter#write(String)

android - 相当于 'configurations.properties'文件的Gradle

android - Vitamio 库使 apk 文件太大

android - ImageView scaleType ="fitXY"在 Cardview 内的 Lollipop 前设备中不起作用 |安卓

android - CardViews 之间的差距在滚动时增加