【问题标题】:When I change the background of a Card view, the corner radius is reset当我更改卡片视图的背景时,角半径被重置
【发布时间】:2018-09-24 23:26:28
【问题描述】:

情况是,当我从程序修改Card 视图背景时,Card 视图的角半径会重置。但是为什么呢?
(我认为我不需要提供任何其他信息(代码、结果图片等),因为我认为它足够清晰,可以理解。如果您需要更多信息,那么您应该写在评论中。)

【问题讨论】:

    标签: android view background android-cardview cornerradius


    【解决方案1】:

    如果您尝试使用具有圆角半径的 CardView,您将在动态设置背景颜色时遇到此问题。请使用yourCardView.setCardBackgroundColor() 方法而不是yourCardView.setBackgroundColor() :)

    【讨论】:

      【解决方案2】:

      我找到了问题的解决方案。
      我需要检索视图的背景并设置其颜色,然后将新背景分配给视图。

      Drawable backgroundOff = v.getBackground(); //v is a view
      backgroundOff.setTint(defaultColor); //defaultColor is an int 
      v.setBackground(backgroundOff);
      

      (这个答案有帮助:https://stackoverflow.com/a/18394982/9377499

      【讨论】:

      • 例如,当您想更改 RecyclerView 内的所有其他 CardView 的颜色并保留圆角时,它就像一个魅力。在此过程中,您只能以android.view.View itemViews 的形式访问CardView 元素,这些元素具有setBackgroundColor() 方法(失败)但没有setCardBackgroundColor() 方法可用。
      【解决方案3】:

      同样的问题,用这种方法解决问题

      首先,创建名为 Drawable "shape_background_cardview" 的形状,并添加以下代码

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

      第二步,自己设置背景为CardView的形状

      yourCardView.setBackgroundResource(R.drawable.shape_background_cardview);
      

      祝你好运

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多