【问题标题】:CardView background changing when changing visibility?CardView背景在改变可见性时改变?
【发布时间】:2016-07-20 23:33:13
【问题描述】:

点击点击编辑文本TextView时,我必须显示CardView,点击外部将隐藏CardView。我试图将CardView 背景切换为透明和白色,但它不起作用。然后我尝试将包含字母的EditTextCardView 放入FrameLayout 然后隐藏并显示CardViewCardView 背景样式更改为透明同时这样做。 你可以在下面找到我的 xml。 请告诉我这里发生了什么?

XML

<FrameLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="@dimen/margin_padding_2">

<android.support.v7.widget.CardView
  android:id="@+id/cardView"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="0dp"
  app:cardCornerRadius="@dimen/box_radius"
  app:cardElevation="0dp"
  app:contentPadding="@dimen/margin_padding_2" />

<com.package.views.EditText
  android:id="@+id/note"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@null"
  android:cursorVisible="false"
  android:gravity="start"
  android:lineSpacingExtra="@dimen/margin_padding_1"
  android:padding="@dimen/margin_padding_2"
  android:textColor="@color/colorWhite"
  android:textSize="@dimen/text_size_5"
  app:textStyle="bold" />

</FrameLayout>

代码

 private void controlView() {
        mCard.setVisibility(View.VISIBLE);
        mNote.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary));
        mNote.setCursorVisible(true);
        mNote.setSelection(mNote.length());
 }

【问题讨论】:

    标签: android android-layout android-studio visibility android-cardview


    【解决方案1】:

    尝试使用

    mCard.setCardBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary));
    

    【讨论】:

    • mNote 是我的EditText。我在问CardView
    • 我很高兴它有帮助..!! :)
    【解决方案2】:

    要以编程方式更改 cardview 的颜色,您可以使用:

    mCard.setCardBackgroundColor(Color.TRANSPARENT);
    

    在 XML 中做的事情

    mCard:cardBackgroundColor="@android:color/transparent"
    

    【讨论】:

    • 感谢您的赞赏。@Stella
    • 两个答案似乎都是正确的,但 janki gadhiya 已先发布,所以我接受了,我也对你的答案投了赞成票。谢谢
    • 但我得到了方法setCardBackgroundColor :)
    猜你喜欢
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2011-08-05
    • 1970-01-01
    • 2011-04-16
    • 2021-09-03
    • 1970-01-01
    相关资源
    最近更新 更多