[英]Android - CardView background always grey
I am trying to change the CardView color programmatically. 我正在尝试以编程方式更改CardView颜色。
This is my CardView: 这是我的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());
where sub.getColor()
returns in this specific case this color: 在这种情况下, sub.getColor()
返回此颜色:
<color name="color_black">#000000</color>
which should be pitch black. 应该是黑色的。 Still my CardView looks like this: 仍然我的CardView看起来像这样:
any idea on why this happens and how to fix it? 关于为什么会发生以及如何解决的任何想法?
I assume problem arising from sub.getColor() . 我假设问题来自sub.getColor() 。 At first properly return Color code . 首先, 正确返回颜色代码。
You can try with this 你可以尝试一下
cardView.setCardBackgroundColor(Color.parseColor("#000000"));
Or 要么
cardView.setCardBackgroundColor(getResources().getColor(R.color.color_black));
The problem is sub.getColor() , you return color id(R.color.color_black) not color code. 问题是sub.getColor(),您返回颜色id(R.color.color_black)而不是颜色代码。 see my code below 请参阅下面的代码
sample.xml 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 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;
}
}
}
Wrong Way : 错误道 :
private int getColorWrongWay()
{
return mColor;
}
Correct Way : 正确方法:
private int getColorCrtWay()
{
return getResources().getColor(mColor);
}
Output : 输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.