Android - CardView 背景总是灰色
Android - CardView background always grey
我正在尝试以编程方式更改 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">
这就是我设置背景颜色的方式:
CardView card = (CardView) findViewById(R.id.createsub_card);
card.setCardBackgroundColor(sub.getColor());
其中 sub.getColor()
returns 在这种特定情况下这种颜色:
<color name="color_black">#000000</color>
应该是一片漆黑。我的 CardView 仍然是这样的:
知道为什么会发生这种情况以及如何解决它吗?
我假设问题是由 sub.getColor() 引起的。首先正确return颜色代码.
你可以试试这个
cardView.setCardBackgroundColor(Color.parseColor("#000000"));
或
cardView.setCardBackgroundColor(getResources().getColor(R.color.color_black));
问题是 sub.getColor() ,你 return 颜色 id(R.color.color_black) 不是颜色代码。请参阅下面的代码
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);
}
输出:
我正在尝试以编程方式更改 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">
这就是我设置背景颜色的方式:
CardView card = (CardView) findViewById(R.id.createsub_card);
card.setCardBackgroundColor(sub.getColor());
其中 sub.getColor()
returns 在这种特定情况下这种颜色:
<color name="color_black">#000000</color>
应该是一片漆黑。我的 CardView 仍然是这样的:
知道为什么会发生这种情况以及如何解决它吗?
我假设问题是由 sub.getColor() 引起的。首先正确return颜色代码.
你可以试试这个
cardView.setCardBackgroundColor(Color.parseColor("#000000"));
或
cardView.setCardBackgroundColor(getResources().getColor(R.color.color_black));
问题是 sub.getColor() ,你 return 颜色 id(R.color.color_black) 不是颜色代码。请参阅下面的代码
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);
}
输出: