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