Android-CardView背景始终为灰色 [英] Android - CardView background always grey

查看:501
本文介绍了Android-CardView背景始终为灰色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式更改CardView颜色.

I am trying to change the CardView color programmatically.

这是我的CardView:

This is my CardView:

<android.support.v7.widget.CardView
    android:id="@+id/createsub_card"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp">

这是我设置背景颜色的方式:

and this is how I set the background color:

CardView card = (CardView) findViewById(R.id.createsub_card);
    card.setCardBackgroundColor(sub.getColor());

其中sub.getColor()在这种情况下会返回以下颜色:

where sub.getColor() returns in this specific case this color:

<color name="color_black">#000000</color>

应为黑色.仍然我的CardView看起来像这样:

which should be pitch black. Still my CardView looks like this:

关于为什么会发生这种情况以及如何解决的任何想法?

推荐答案

问题是sub.getColor(),您返回颜色id(R.color.color_black)而不是颜色代码.在下面查看我的代码

The problem is sub.getColor() , you return color id(R.color.color_black) not color code. see my code below

sample.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.CardView
        android:id="@+id/createsub_card"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_margin="10dp">


    </android.support.v7.widget.CardView>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Click"
        android:layout_below="@+id/createsub_card"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="13dp" />
</RelativeLayout>

SampleActivity.java

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.Button;

/**
 * Created by Magesh on 5/4/2017.
 */

public class SampleActivity extends AppCompatActivity implements View.OnClickListener
{
    private CardView mCardView;
    private Button mBtnClick;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample);
        mCardView = (CardView) findViewById(R.id.createsub_card);
        mBtnClick = (Button) findViewById(R.id.button);
        mBtnClick.setOnClickListener(this);
       // mCardView.setCardBackgroundColor(getResources().getColorWrongWay(R.color.color_black));
        setColor(R.color.color_black);//hex color code id #000000
        mCardView.setCardBackgroundColor(getColorWrongWay());// you set like this
    }


    private int mColor = 0;
    private void setColor(int color)
    {
        mColor = color;
    }

    private int getColorWrongWay()
    {
        return mColor;
    }

    private int getColorCrtWay()
    {
        return getResources().getColor(mColor);
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.button:
            {
                mCardView.setCardBackgroundColor(getColorCrtWay());// should be like this.
            }
            break;
        }
    }
}

错误的方式:

private int getColorWrongWay()
{
    return mColor;
}

正确的方式:

 private int getColorCrtWay()
{
    return getResources().getColor(mColor);
}

输出:

这篇关于Android-CardView背景始终为灰色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆