如何以编程方式显示背景图像视图

时间:2014-02-11 07:23:23

标签: android

relativeLayout.setBackgroundResource(R.drawable.b);
setContentView(relativeLayout);

img1.setImageResource(R.drawable.d);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(100,100);

img1.setLayoutParams(parms);
setContentView(img1, parms);

(部分)

我设置了默认背景,并希望在5秒后更改此b.png。这些行在5秒处理程序方法之后。以上两行让我在背景中看到新的b.png。但是当我在上面设置imageview时会出错。我还尝试了ImageView在原点添加xml并显示为view.visible。但它不能。只有背景图像可用,并且没有重叠图像被拒绝。

1 个答案:

答案 0 :(得分:0)

试试这个

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    RelativeLayout relativeLayout = new RelativeLayout(this);
    ImageView img1 = new ImageView(this);

    relativeLayout.setBackgroundResource(R.drawable.b); 
    setContentView(relativeLayout);

    img1.setImageResource(R.drawable.d); 
    RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(100,100);

    img1.setLayoutParams(parms); 
    relativeLayout.addView(img1, parms);
}