所有。我有个问题。我想按下按钮调整图像大小。 这是活动中的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
currentImage = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(400, 400);
currentImage.setLayoutParams(params);
images = getResources().obtainTypedArray(R.array.images);
currentImage.setImageResource(R.drawable.bronar_jenson);
RelativeLayout l = (RelativeLayout)findViewById(R.id.layout);
l.addView(currentImage);
b = (Button)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentImage.getLayoutParams().width = 20;
}
});
}
不幸的是它没有按预期工作 - 按钮没有调整大小。提前谢谢。
答案 0 :(得分:2)
这应该有效:
替换:
currentImage.getLayoutParams().width = 20;
使用:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(20, 400);
l.updateViewLayout(currentImage, params);
你可能需要让RelativeLayout l final