自定义通知中的textview背景不会更改

时间:2019-03-28 12:20:33

标签: java android textview android-notifications

好吧,我有一个自定义通知,其中包含4个textview,我想以编程方式为其中的一个设置背景(该textview在xml中默认为椭圆形背景和笔触,而其他背景则为简单的彩色背景)。可以更改“ 3个简单的彩色”文本视图的背景,但是当我尝试更改“椭圆形”的背景时,它根本不会改变。

这就是我在做什么:

showNotification.java:

private void changeColor() {
    LayoutInflater.from(context).inflate(R.layout.custom_notification, null);
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    ViewGroup custom_notification = (ViewGroup) 
    inflater.inflate(contentView.getLayoutId(), null);
    // this section changes notification background
    // and gets the color from shared preference
    String colorString;
    int color;
    colorString = sharedPreferenceManager.getStringFromSP(CustomColorPickerDialog.SP_KEY_NOTIFICATION_BG_COLOR
                , Integer.toHexString(ContextCompat.getColor(context, R.color.default_notification_bg_color)));
    color = Color.parseColor("#" + colorString);
    contentView.setInt(custom_notification.getId(), "setBackgroundColor", color);

    // this section is not working
    colorString = sharedPreferenceManager.getStringFromSP(CustomColorPickerDialog.SP_KEY_NOTIFICATION_PERCENT_BG_COLOR
                , Integer.toHexString(ContextCompat.getColor(context, R.color.default_notification_bg_color)));
    color = Color.parseColor("#" + colorString);
    String ringColorString = sharedPreferenceManager.getStringFromSP(CustomColorPickerDialog.SP_KEY_NOTIFICATION_PERCENT_RING_COLOR
                , Integer.toHexString(ContextCompat.getColor(context, R.color.colorPrimaryDark_20)));
    int ringColor = Color.parseColor("#" + ringColorString);
    custom_notification.getChildAt(1).setBackground(new DrawShape(color, ringColor));
    // DrawShape is a custom class
}

DrawShape.java:

public class DrawShape extends Drawable {

    private Paint mFillPaint, mStrokePaint;
    private int mRadius = 0;


    public DrawShape(int color, int ringColor) {

        this.mFillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        this.mFillPaint.setStyle(Paint.Style.FILL);
        this.mFillPaint.setColor(color);

        this.mStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        this.mStrokePaint.setStyle(Paint.Style.STROKE);
        this.mStrokePaint.setStrokeWidth(10);
        this.mStrokePaint.setColor(ringColor);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        final Rect bounds = getBounds();
        canvas.drawCircle(bounds.centerX(), bounds.centerY(), mRadius - 5, mFillPaint);
        canvas.drawCircle(bounds.centerX(), bounds.centerY(), mRadius - 5, mStrokePaint);

    }

    @Override
    protected void onBoundsChange(final Rect bounds) {
        super.onBoundsChange(bounds);
        mRadius = Math.min(bounds.width(), bounds.height()) / 2;
    }

    @Override
    public void setAlpha(int alpha) {
        mStrokePaint.setAlpha(alpha);
        mFillPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        mStrokePaint.setColorFilter(colorFilter);
        mFillPaint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

Notification_layout.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/notification_layout"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:padding="10dp"
    android:background="@color/default_notification_bg_color">

    <TextView
        android:id="@+id/notification_percent_txtview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="10dp"
        android:paddingTop="4dp"
        android:background="@drawable/notif_percent_background"
        tools:text="60"
        android:textAlignment="center"
        android:textSize="28sp"
        android:textColor="@android:color/white"/>

<RelativeLayout/>

notif_percent_background.xml

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid
        android:color="@color/default_notification_bg_color"/>

    <stroke
        android:color="@color/colorPrimaryDark_20"
        android:width="1dp"/>

    <size
        android:width="50dp"
        android:height="50dp"/>
</shape>

因此,当我尝试更改笔触颜色和形状背景时,什么也没发生(仍显示默认背景)

我什至尝试过,但是什么也没发生:

custom_notification.findViewById(R.id.notification_percent_txtview).setBackground(new DrawShape(color, ringColor));

欢迎任何建议和解决方案:-)

0 个答案:

没有答案