设置textview的背景颜色

时间:2018-03-30 10:04:51

标签: android xml

我有一个带有背景圆角的textview,我尝试以编程方式更改颜色背景,但它不起作用。

我的代码:

      <TextView
                android:id="@+id/incident_icon"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:background="@drawable/rounded_corner"
                android:backgroundTint="@color/soft_grey"
                android:clickable="true"
                android:gravity="center"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="2dp"
                android:layout_marginRight="2dp"
                android:text="Inc"
                android:textColor="@color/white"
                android:textSize="16sp"
                android:textStyle="bold"

                android:visibility="visible" />

对于我的圆角我使用此代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#ffffff" />
    <corners android:radius="50dp" />
</shape>

这是我的java代码,我尝试了这两种方法,但它不起作用:

1- ((TextView) convertView.findViewById(R.id.incident_icon)).setBackgroundColor(mainActivity.getResources().getColor(R.color.honeycombish_blue, null));



2-  convertView.findViewById(R.id. incident_icon).setBackgroundResource(R.color.honeycombish_blue);

4 个答案:

答案 0 :(得分:4)

替换您的文字视图

            <TextView
                android:id="@+id/incident_icon"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_marginEnd="2dp"
                android:layout_marginRight="2dp"
                android:layout_marginTop="8dp"
                android:background="@drawable/rounded_corner"
                android:clickable="true"
                android:gravity="center"
                android:text="Inc"
                android:textColor="@color/white"
                android:textSize="16sp"
                android:textStyle="bold"
                android:visibility="visible" />

您的 Rounded_corner 文件rounded_corner.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#ffffff" />
    <corners android:radius="50dp" />
</shape>

更改您的Java代码,如下所示。

TextView incident_icon = (TextView) v.findViewById(R.id.incident_icon);
Drawable mDrawable = ContextCompat.getDrawable(mActivity, R.drawable.rounded_corner);
mDrawable.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(mActivity, R.color.honeycombish_blue), PorterDuff.Mode.SRC_IN));
incident_icon.setBackground(mDrawable);

答案 1 :(得分:1)

尝试:

在drawable honeycombish_blue.xml

中创建新文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#6666FF" />
    <corners android:radius="50dp" />
</shape>

这里使用android:color值为蓝色。而不是白色

在java文件中写

convertView.setBackgroundResource(R.drawable.honeycombish_blue);

从xml中删除backgroundTint

答案 2 :(得分:0)

您正在为Textview设置一个可绘制文件。

android:background="@drawable/rounded_corner"

在你的java代码中,我认为你需要设置

.setBackground(R.drawable.honeycombish_blue);

简而言之,制作一个不同颜色的新drawable文件并将其设置在TextView上。

答案 3 :(得分:0)

感谢@Nikunj Patel的回答

GradientDrawable mDrawable = (GradientDrawable) ContextCompat.getDrawable(getActivity(), R.drawable.DRAWABLE_LAYOUT);
                    mDrawable.setColor(getContext().getResources().getColor(R.color.white));
                    YOUR_TEXT_VIEW.setTextColor(getContext().getResources().getColor(R.color.black));
                    YOUR_TEXT_VIEW.setBackground(mDrawable);
相关问题