Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在显示之前以编程方式编辑android视图的宽度_Android - Fatal编程技术网

无法在显示之前以编程方式编辑android视图的宽度

无法在显示之前以编程方式编辑android视图的宽度,android,Android,我试图编辑文本视图的宽度,然后根据某个分数值将其加载到屏幕上。该要求类似于,基于相对分数值,文本视图应该具有宽度 我已经在活动的onCreate()方法中编写了以下代码,如下所示 {{{ }}} 它像宝石一样工作!!但是,当我想对多个TextView对象执行此操作时,它不会更改TextView对象的宽度 有人能帮我更改多个TextView对象的宽度吗 谢谢,不要使用GlobalLayoutListener,请尝试覆盖活动的onMeasure方法 TextView graph = (TextVie

我试图编辑文本视图的宽度,然后根据某个分数值将其加载到屏幕上。该要求类似于,基于相对分数值,文本视图应该具有宽度

我已经在活动的onCreate()方法中编写了以下代码,如下所示

{{{

}}}

它像宝石一样工作!!但是,当我想对多个TextView对象执行此操作时,它不会更改TextView对象的宽度

有人能帮我更改多个TextView对象的宽度吗


谢谢,

不要使用
GlobalLayoutListener
,请尝试覆盖活动的
onMeasure
方法

TextView graph = (TextView) findViewById(R.id.predictionScoreGraph);

    graph.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    int score = 60;

                    UserData data = UserData.getInstance();
                    score = data.overallScore();
                    TextView graph = (TextView) findViewById(R.id.predictionScoreGraph);

                    int grossWidth = graph.getWidth();

                    Log.d(TAG, "Existing width of the text view is "
                            + grossWidth);

                    LayoutParams existing = graph.getLayoutParams();
                    int fillWidth = (grossWidth * score) / 100;

                    Log.d(TAG, "Modified width is " + fillWidth);
                    graph.setWidth(fillWidth);
                    existing.width = fillWidth;
                    graph.setLayoutParams(existing);
                    graph.setBackgroundColor(getResources().getColor(
                            android.R.color.holo_green_light));


                    graph.getViewTreeObserver()
                            .removeGlobalOnLayoutListener(this);

                }
            });