【问题标题】:SeekBar is not changing custom progress drawable through Java CodeSeekBar 不会通过 Java 代码更改可绘制的自定义进度
【发布时间】:2021-05-12 08:07:56
【问题描述】:

我有可用于搜索栏的自定义进度。如果我将进度可绘制在 xml 文件中,它工作正常,但是当我尝试通过 Java 代码更改它的可绘制进度时,它什么也没显示。有时会出现但不稳定。

我的 SeekBar XML 代码:

        <SeekBar
            android:id="@+id/seekMcqBar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:progress="0"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:progressDrawable="@drawable/progress_tract_unselected"
            android:thumb="@android:color/transparent"
            app:layout_constraintTop_toBottomOf="@+id/mcqQuestionTv"

我的自定义进度 Drawable: 对于未选择的选项:progress_tract_unselected

<item>
    <shape android:shape="rectangle">
        <solid android:color="@android:color/transparent"/>
        <size android:height="40dp"/>
        <corners android:radius="4dp"/>
        <stroke android:color="#E0E0E0"
            android:width="1dp"/>
    </shape>
</item>

<item>
    <scale android:scaleWidth="100%">
        <selector>
            <item android:state_enabled="false"
                android:drawable="@android:color/transparent"/>
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="#DDDDDD"/>
                    <size android:height="40dp"/>
                    <corners android:bottomLeftRadius="4dp"
                        android:topLeftRadius="4dp"/>
                    <stroke android:color="#E0E0E0"
                        android:width="1dp"/>
                </shape>
            </item>
        </selector>
    </scale>
</item>

对于选定和正确的选项:progress_tract_correct_option

<item>
    <shape android:shape="rectangle">
        <solid android:color="@android:color/transparent"/>
        <size android:height="40dp"/>
        <corners android:radius="4dp"/>
        <stroke android:color="#E0E0E0"
            android:width="1dp"/>
    </shape>
</item>

<item>
    <scale
        android:layout_width="wrap_content"
        android:scaleWidth="100%">
        <selector>
            <item
                android:drawable="@android:color/transparent"
                android:state_enabled="false" />
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@color/option_correct" />
                    <size android:height="40dp" />
                    <corners
                        android:bottomLeftRadius="4dp"
                        android:topLeftRadius="4dp" />
                    <stroke
                        android:width="1dp"
                        android:color="@color/colorPrimary" />
                </shape>
            </item>
        </selector>
    </scale>
</item>

我的用于更改可绘制对象的 Java 代码:

    String answer = modelAskQuestionAdmin.getAnswer();
    String option1 = modelAskQuestionAdmin.getOption1();
    String option2 = modelAskQuestionAdmin.getOption2();
    String option3 = modelAskQuestionAdmin.getOption3();
    String option4 = modelAskQuestionAdmin.getOption4();
    if(answer.equals(option1)){
        
     holder.option1MCQTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_option1_primary_color, 0, 0,0);
        holder.seekMcqBar1.setProgressDrawable(context.getResources().getDrawable(R.drawable.progress_tract_correct_option));
        holder.option1PercentTv.setTextColor(context.getResources().getColor(R.color.colorPrimary));
    }
    else  if(answer.equals(option2)){
        holder.option2MCQTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_option2_color_primary, 0, 0,0);
        holder.seekMcqBar2.setProgressDrawable(context.getResources().getDrawable(R.drawable.progress_tract_correct_option));
        holder.option2PercentTv.setTextColor(context.getResources().getColor(R.color.colorPrimary));

    }
    else  if(answer.equals(option3)){
        holder.option3MCQTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_option3_color_primary, 0, 0,0);
        holder.seekMcqBar3.setProgressDrawable(context.getResources().getDrawable(R.drawable.progress_tract_correct_option));
        holder.option3PercentTv.setTextColor(context.getResources().getColor(R.color.colorPrimary));

    }
    else  if(answer.equals(option4)){
        holder.option4MCQTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_option4_color_primary, 0, 0,0);
        holder.seekMcqBar4.setProgressDrawable(context.getResources().getDrawable(R.drawable.progress_tract_correct_option));
        holder.option4PercentTv.setTextColor(context.getResources().getColor(R.color.colorPrimary));

    }

但结果是这样的:

图片: 图片option 2nd must be green.

但它什么也没显示,我的意思是它没有显示进度条。

我错过了什么吗? 帮我! 它可能是重复的帖子,但我无法与其他帖子相关联。 等待回复!..

【问题讨论】:

标签: progress-bar android-drawable seekbar


【解决方案1】:

我已经试过了,但这也不起作用

矩形边界2 = holder.seekMcqBar2.getProgressDrawable().getBounds();
holder.seekMcqBar2.setProgressDrawable(context.getResources().getDrawable(R.drawable.progress_tract_correct_option)); holder.seekMcqBar2.getProgressDrawable().setBounds(bounds2);

但我找到的唯一解决方案是:

        holder.seekMcqBar2.getProgressDrawable().setColorFilter(Color.parseColor("#8000897B"), PorterDuff.Mode.SRC_IN);

但是在这个我们必须手动放置颜色...

希望任何人对这个问题有任何即兴的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    相关资源
    最近更新 更多